( fileName: string, oldStr: string, aiStr: string, userStr: string, )
| 14 | }; |
| 15 | |
| 16 | export function getDiffStat( |
| 17 | fileName: string, |
| 18 | oldStr: string, |
| 19 | aiStr: string, |
| 20 | userStr: string, |
| 21 | ): DiffStat { |
| 22 | const countLines = (patch: Diff.ParsedDiff) => { |
| 23 | let added = 0; |
| 24 | let removed = 0; |
| 25 | patch.hunks.forEach((hunk: Diff.Hunk) => { |
| 26 | hunk.lines.forEach((line: string) => { |
| 27 | if (line.startsWith('+')) { |
| 28 | added++; |
| 29 | } else if (line.startsWith('-')) { |
| 30 | removed++; |
| 31 | } |
| 32 | }); |
| 33 | }); |
| 34 | return { added, removed }; |
| 35 | }; |
| 36 | |
| 37 | const patch = Diff.structuredPatch( |
| 38 | fileName, |
| 39 | fileName, |
| 40 | oldStr, |
| 41 | aiStr, |
| 42 | 'Current', |
| 43 | 'Proposed', |
| 44 | DEFAULT_DIFF_OPTIONS, |
| 45 | ); |
| 46 | const { added: aiAddedLines, removed: aiRemovedLines } = countLines(patch); |
| 47 | |
| 48 | const userPatch = Diff.structuredPatch( |
| 49 | fileName, |
| 50 | fileName, |
| 51 | aiStr, |
| 52 | userStr, |
| 53 | 'Proposed', |
| 54 | 'User', |
| 55 | DEFAULT_DIFF_OPTIONS, |
| 56 | ); |
| 57 | const { added: userAddedLines, removed: userRemovedLines } = |
| 58 | countLines(userPatch); |
| 59 | |
| 60 | return { |
| 61 | ai_added_lines: aiAddedLines, |
| 62 | ai_removed_lines: aiRemovedLines, |
| 63 | user_added_lines: userAddedLines, |
| 64 | user_removed_lines: userRemovedLines, |
| 65 | }; |
| 66 | } |
no test coverage detected