Skip to content

Commit

Permalink
Add: add toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
tennashi committed Sep 20, 2021
1 parent c2adccc commit 85df5d7
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ type Line = {

const hunkHeaderRegexp = /@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@/

export function toString(diff: Diff): string {
if (diff.hunks.length === 0) {
return "";
}
let res: string
res = '--- ' + diff.beforeFileName + '\n'
res += '+++ ' + diff.afterFileName + '\n'
diff.hunks.forEach((hunk: Hunk) => {
res += '@@ -' + hunk.header.beforeStartLine + ',' + hunk.header.beforeLines + ' +' + hunk.header.afterStartLine + ',' + hunk.header.afterLines + ' @@\n'
hunk.lines.forEach((line: Line) => {
switch (line.mark) {
case 'add':
res += '+';
break;
case 'delete':
res += '-';
break;
case 'nomodified':
res += ' ';
break;
}
res += line.text + '\n';
})
})

return res;
}

export function parse(text: string): Diff[] {
const diffs: Diff[] = [];
let currentDiffIndex = 0;
Expand Down

0 comments on commit 85df5d7

Please sign in to comment.