|
1 | | -import { createTwoFilesPatch } from 'diff' |
| 1 | +import { diffLines } from 'diff' |
2 | 2 |
|
3 | 3 | export const get_diff_stats = (params: { |
4 | 4 | original_content: string |
5 | 5 | new_content: string |
6 | 6 | }): { lines_added: number; lines_removed: number } => { |
7 | | - const original_content_processed = params.original_content |
8 | | - .split(/\r?\n/) |
9 | | - .map((line) => line.trim()) |
10 | | - .join('\n') |
11 | | - const new_content_processed = params.new_content |
12 | | - .split(/\r?\n/) |
13 | | - .map((line) => line.trim()) |
14 | | - .join('\n') |
15 | | - if (original_content_processed === new_content_processed) { |
| 7 | + const original_content_processed = params.original_content.replace( |
| 8 | + /\r\n/g, |
| 9 | + '\n' |
| 10 | + ) |
| 11 | + const new_content_processed = params.new_content.replace(/\r\n/g, '\n') |
| 12 | + |
| 13 | + if (original_content_processed == new_content_processed) { |
16 | 14 | return { lines_added: 0, lines_removed: 0 } |
17 | 15 | } |
18 | 16 |
|
19 | | - const patch = createTwoFilesPatch( |
20 | | - 'original', |
21 | | - 'modified', |
22 | | - original_content_processed, |
23 | | - new_content_processed, |
24 | | - undefined, |
25 | | - undefined, |
26 | | - { context: 0 } |
27 | | - ) |
| 17 | + const changes = diffLines(original_content_processed, new_content_processed) |
28 | 18 |
|
29 | 19 | let lines_added = 0 |
30 | 20 | let lines_removed = 0 |
31 | 21 |
|
32 | | - const lines = patch.split('\n') |
33 | | - for (const line of lines) { |
34 | | - if (line.startsWith('+') && !line.startsWith('+++')) { |
35 | | - lines_added++ |
36 | | - } else if (line.startsWith('-') && !line.startsWith('---')) { |
37 | | - lines_removed++ |
| 22 | + for (const change of changes) { |
| 23 | + if (change.added) { |
| 24 | + lines_added += change.count || 0 |
| 25 | + } else if (change.removed) { |
| 26 | + lines_removed += change.count || 0 |
38 | 27 | } |
39 | 28 | } |
40 | 29 |
|
|
0 commit comments