Skip to content

Commit 7760e23

Browse files
authored
Merge pull request #144 from soulbeing/SpliteFile
Split file using all possible line delimiter instead of hard-coded "/n" and join lines back using the original delimiters
2 parents e4ee3bd + 563e502 commit 7760e23

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

Diff for: src/patch/apply.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export function applyPatch(source, uniDiff, options = {}) {
1515
}
1616

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

2122
compareLine = options.compareLine || ((lineNumber, line, operation, patchContent) => line === patchContent),
@@ -86,15 +87,18 @@ export function applyPatch(source, uniDiff, options = {}) {
8687
for (let j = 0; j < hunk.lines.length; j++) {
8788
let line = hunk.lines[j],
8889
operation = line[0],
89-
content = line.substr(1);
90+
content = line.substr(1),
91+
delimiter = hunk.linedelimiters[j];
9092

9193
if (operation === ' ') {
9294
toPos++;
9395
} else if (operation === '-') {
9496
lines.splice(toPos, 1);
97+
delimiters.splice(toPos, 1);
9598
/* istanbul ignore else */
9699
} else if (operation === '+') {
97100
lines.splice(toPos, 0, content);
101+
delimiters.splice(toPos, 0, delimiter);
98102
toPos++;
99103
} else if (operation === '\\') {
100104
let previousOperation = hunk.lines[j - 1] ? hunk.lines[j - 1][0] : null;
@@ -111,11 +115,16 @@ export function applyPatch(source, uniDiff, options = {}) {
111115
if (removeEOFNL) {
112116
while (!lines[lines.length - 1]) {
113117
lines.pop();
118+
delimiters.pop();
114119
}
115120
} else if (addEOFNL) {
116121
lines.push('');
122+
delimiters.push('\n');
123+
}
124+
for (let _k = 0; _k < lines.length - 1; _k++) {
125+
lines[_k] = lines[_k] + delimiters[_k];
117126
}
118-
return lines.join('\n');
127+
return lines.join('');
119128
}
120129

121130
// Wrapper that supports multiple file patches via callbacks.

Diff for: src/patch/parse.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export function parsePatch(uniDiff, options = {}) {
2-
let diffstr = uniDiff.split('\n'),
2+
let diffstr = uniDiff.split(/\r\n|[\n\v\f\r\x85]/),
3+
delimiters = uniDiff.match(/\r\n|[\n\v\f\r\x85]/g) || [],
34
list = [],
45
i = 0;
56

@@ -75,7 +76,8 @@ export function parsePatch(uniDiff, options = {}) {
7576
oldLines: +chunkHeader[2] || 1,
7677
newStart: +chunkHeader[3],
7778
newLines: +chunkHeader[4] || 1,
78-
lines: []
79+
lines: [],
80+
linedelimiters: []
7981
};
8082

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

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

97100
if (operation === '+') {
98101
addCount++;

Diff for: test/patch/parse.js

+65
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ describe('patch/parse', function() {
2121
' line3',
2222
'+line4',
2323
' line5'
24+
],
25+
linedelimiters: [
26+
'\n',
27+
'\n',
28+
'\n',
29+
'\n'
2430
]
2531
}
2632
]
@@ -39,6 +45,10 @@ describe('patch/parse', function() {
3945
lines: [
4046
'-line3',
4147
'+line4'
48+
],
49+
linedelimiters: [
50+
'\n',
51+
'\n'
4252
]
4353
}
4454
]
@@ -66,6 +76,12 @@ describe('patch/parse', function() {
6676
' line3',
6777
'+line4',
6878
' line5'
79+
],
80+
linedelimiters: [
81+
'\n',
82+
'\n',
83+
'\n',
84+
'\n'
6985
]
7086
},
7187
{
@@ -76,6 +92,12 @@ describe('patch/parse', function() {
7692
' line3',
7793
'-line4',
7894
' line5'
95+
],
96+
linedelimiters: [
97+
'\n',
98+
'\n',
99+
'\n',
100+
'\n'
79101
]
80102
}
81103
]
@@ -107,6 +129,12 @@ describe('patch/parse', function() {
107129
' line3',
108130
'+line4',
109131
' line5'
132+
],
133+
linedelimiters: [
134+
'\n',
135+
'\n',
136+
'\n',
137+
'\n'
110138
]
111139
}
112140
]
@@ -147,6 +175,12 @@ Index: test2
147175
' line3',
148176
'+line4',
149177
' line5'
178+
],
179+
linedelimiters: [
180+
'\n',
181+
'\n',
182+
'\n',
183+
'\n'
150184
]
151185
}
152186
]
@@ -165,6 +199,12 @@ Index: test2
165199
' line3',
166200
'+line4',
167201
' line5'
202+
],
203+
linedelimiters: [
204+
'\n',
205+
'\n',
206+
'\n',
207+
'\n'
168208
]
169209
}
170210
]
@@ -201,6 +241,12 @@ Index: test2
201241
' line3',
202242
'+line4',
203243
' line5'
244+
],
245+
linedelimiters: [
246+
'\n',
247+
'\n',
248+
'\n',
249+
'\n'
204250
]
205251
}
206252
]
@@ -218,6 +264,12 @@ Index: test2
218264
' line3',
219265
'+line4',
220266
' line5'
267+
],
268+
linedelimiters: [
269+
'\n',
270+
'\n',
271+
'\n',
272+
'\n'
221273
]
222274
}
223275
]
@@ -237,6 +289,10 @@ Index: test2
237289
lines: [
238290
'-line5',
239291
'\\ No newline at end of file'
292+
],
293+
linedelimiters: [
294+
'\n',
295+
'\n'
240296
]
241297
}
242298
]
@@ -255,6 +311,10 @@ Index: test2
255311
lines: [
256312
'+line5',
257313
'\\ No newline at end of file'
314+
],
315+
linedelimiters: [
316+
'\n',
317+
'\n'
258318
]
259319
}
260320
]
@@ -275,6 +335,11 @@ Index: test2
275335
'+line4',
276336
' line5',
277337
'\\ No newline at end of file'
338+
],
339+
linedelimiters: [
340+
'\n',
341+
'\n',
342+
'\n'
278343
]
279344
}
280345
]

0 commit comments

Comments
 (0)