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

Split file using all possible line delimiter instead of hard-coded "/n" and join lines back using the original delimiters #144

Merged
merged 7 commits into from
Nov 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 12 additions & 3 deletions src/patch/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export function applyPatch(source, uniDiff, options = {}) {
}

// Apply the diff to the input
let lines = source.split('\n'),
let lines = source.split(/\r\n|[\n\v\f\r\x85]/),
delimiters = source.match(/\r\n|[\n\v\f\r\x85]/g) || [],
hunks = uniDiff.hunks,

compareLine = options.compareLine || ((lineNumber, line, operation, patchContent) => line === patchContent),
Expand Down Expand Up @@ -86,15 +87,18 @@ export function applyPatch(source, uniDiff, options = {}) {
for (let j = 0; j < hunk.lines.length; j++) {
let line = hunk.lines[j],
operation = line[0],
content = line.substr(1);
content = line.substr(1),
delimiter = hunk.linedelimiters[j];

if (operation === ' ') {
toPos++;
} else if (operation === '-') {
lines.splice(toPos, 1);
delimiters.splice(toPos, 1);
/* istanbul ignore else */
} else if (operation === '+') {
lines.splice(toPos, 0, content);
delimiters.splice(toPos, 0, delimiter);
toPos++;
} else if (operation === '\\') {
let previousOperation = hunk.lines[j - 1] ? hunk.lines[j - 1][0] : null;
Expand All @@ -111,11 +115,16 @@ export function applyPatch(source, uniDiff, options = {}) {
if (removeEOFNL) {
while (!lines[lines.length - 1]) {
lines.pop();
delimiters.pop();
}
} else if (addEOFNL) {
lines.push('');
delimiters.push('\n');
}
for (let _k = 0; _k < lines.length - 1; _k++) {
lines[_k] = lines[_k] + delimiters[_k];
}
return lines.join('\n');
return lines.join('');
}

// Wrapper that supports multiple file patches via callbacks.
Expand Down
7 changes: 5 additions & 2 deletions src/patch/parse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export function parsePatch(uniDiff, options = {}) {
let diffstr = uniDiff.split('\n'),
let diffstr = uniDiff.split(/\r\n|[\n\v\f\r\x85]/),
delimiters = uniDiff.match(/\r\n|[\n\v\f\r\x85]/g) || [],
list = [],
i = 0;

Expand Down Expand Up @@ -75,7 +76,8 @@ export function parsePatch(uniDiff, options = {}) {
oldLines: +chunkHeader[2] || 1,
newStart: +chunkHeader[3],
newLines: +chunkHeader[4] || 1,
lines: []
lines: [],
linedelimiters: []
};

let addCount = 0,
Expand All @@ -93,6 +95,7 @@ export function parsePatch(uniDiff, options = {}) {

if (operation === '+' || operation === '-' || operation === ' ' || operation === '\\') {
hunk.lines.push(diffstr[i]);
hunk.linedelimiters.push(delimiters[i] || '\n');

if (operation === '+') {
addCount++;
Expand Down
65 changes: 65 additions & 0 deletions test/patch/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe('patch/parse', function() {
' line3',
'+line4',
' line5'
],
linedelimiters: [
'\n',
'\n',
'\n',
'\n'
]
}
]
Expand All @@ -39,6 +45,10 @@ describe('patch/parse', function() {
lines: [
'-line3',
'+line4'
],
linedelimiters: [
'\n',
'\n'
]
}
]
Expand Down Expand Up @@ -66,6 +76,12 @@ describe('patch/parse', function() {
' line3',
'+line4',
' line5'
],
linedelimiters: [
'\n',
'\n',
'\n',
'\n'
]
},
{
Expand All @@ -76,6 +92,12 @@ describe('patch/parse', function() {
' line3',
'-line4',
' line5'
],
linedelimiters: [
'\n',
'\n',
'\n',
'\n'
]
}
]
Expand Down Expand Up @@ -107,6 +129,12 @@ describe('patch/parse', function() {
' line3',
'+line4',
' line5'
],
linedelimiters: [
'\n',
'\n',
'\n',
'\n'
]
}
]
Expand Down Expand Up @@ -147,6 +175,12 @@ Index: test2
' line3',
'+line4',
' line5'
],
linedelimiters: [
'\n',
'\n',
'\n',
'\n'
]
}
]
Expand All @@ -165,6 +199,12 @@ Index: test2
' line3',
'+line4',
' line5'
],
linedelimiters: [
'\n',
'\n',
'\n',
'\n'
]
}
]
Expand Down Expand Up @@ -201,6 +241,12 @@ Index: test2
' line3',
'+line4',
' line5'
],
linedelimiters: [
'\n',
'\n',
'\n',
'\n'
]
}
]
Expand All @@ -218,6 +264,12 @@ Index: test2
' line3',
'+line4',
' line5'
],
linedelimiters: [
'\n',
'\n',
'\n',
'\n'
]
}
]
Expand All @@ -237,6 +289,10 @@ Index: test2
lines: [
'-line5',
'\\ No newline at end of file'
],
linedelimiters: [
'\n',
'\n'
]
}
]
Expand All @@ -255,6 +311,10 @@ Index: test2
lines: [
'+line5',
'\\ No newline at end of file'
],
linedelimiters: [
'\n',
'\n'
]
}
]
Expand All @@ -275,6 +335,11 @@ Index: test2
'+line4',
' line5',
'\\ No newline at end of file'
],
linedelimiters: [
'\n',
'\n',
'\n'
]
}
]
Expand Down