Skip to content

Commit b42b679

Browse files
committed
more changes
Change-Id: I301f5e4822582689116bb5ba61228ed28d0fd1be Signed-off-by: Sidney <[email protected]>
1 parent 17e9a1b commit b42b679

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

src/patch/apply.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export function applyPatch(source, uniDiff, options = {}) {
1515
}
1616

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

2222
compareLine = options.compareLine || ((lineNumber, line, operation, patchContent) => line === patchContent),
@@ -121,7 +121,7 @@ export function applyPatch(source, uniDiff, options = {}) {
121121
lines.push('');
122122
delimiters.push('\n');
123123
}
124-
for (let _k = 0; _k < lines.length; _k++) {
124+
for (let _k = 0; _k < lines.length - 1; _k++) {
125125
lines[_k] = lines[_k] + delimiters[_k];
126126
}
127127
return lines.join('');

src/patch/parse.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export function parsePatch(uniDiff, options = {}) {
2-
let diffstr = uniDiff.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/),
3-
delimiters = uniDiff.match(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g),
2+
let diffstr = uniDiff.split(/\r\n|[\n\v\f\r\x85]/),
3+
delimiters = uniDiff.match(/\r\n|[\n\v\f\r\x85]/g) || [],
44
list = [],
55
i = 0;
66

@@ -95,7 +95,7 @@ export function parsePatch(uniDiff, options = {}) {
9595

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

100100
if (operation === '+') {
101101
addCount++;

test/patch/parse.js

Lines changed: 65 additions & 0 deletions
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)