Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove linedelimiters, improve handling of Windows vs Unix line endings #518

Merged
merged 16 commits into from
Jun 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a util for converting patches between Windows & Unix line endings
ExplodingCabbage committed May 27, 2024

Verified

This commit was signed with the committer’s verified signature.
LZRS L≡ZRS
commit 66b4b068a2a92e4200feeba416e14957d17ffea7
19 changes: 19 additions & 0 deletions src/patch/line-endings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function unixToWin(patch) {
return patch.map(index => ({
...index,
hunks: index.hunks.map(hunk => ({
...hunk,
lines: hunk.lines.map(line => line.endsWith('\r') ? line : line + '\r')
}))
}));
}

export function winToUnix(patch) {
return patch.map(index => ({
...index,
hunks: index.hunks.map(hunk => ({
...hunk,
lines: hunk.lines.map(line => line.endsWith('\r') ? line.substring(0, line.length - 1) : line)
}))
}));
}
47 changes: 47 additions & 0 deletions test/patch/line-endings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {parsePatch} from '../../lib/patch/parse';
import {formatPatch} from '../../lib/patch/create';
import {winToUnix, unixToWin} from '../../lib/patch/line-endings';

import {expect} from 'chai';

describe('unixToWin and winToUnix', function() {
it('should convert line endings in a patch between Unix-style and Windows-style', function() {
const patch = parsePatch(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,3 +1,4 @@\n'
+ ' line2\n'
+ ' line3\r\n'
+ '+line4\r\n'
+ ' line5\n'
);

const winPatch = unixToWin(patch);
expect(formatPatch(winPatch)).to.equal(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,3 +1,4 @@\n'
+ ' line2\r\n'
+ ' line3\r\n'
+ '+line4\r\n'
+ ' line5\r\n'
);

const unixPatch = winToUnix(winPatch);
expect(formatPatch(unixPatch)).to.equal(
'Index: test\n'
+ '===================================================================\n'
+ '--- test\theader1\n'
+ '+++ test\theader2\n'
+ '@@ -1,3 +1,4 @@\n'
+ ' line2\n'
+ ' line3\n'
+ '+line4\n'
+ ' line5\n'
);
});
});