Skip to content

Commit 38a9e09

Browse files
committed
Remove \n from conflict boundaries
(closes #48)
1 parent 3bedfe0 commit 38a9e09

4 files changed

Lines changed: 95 additions & 75 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,24 @@ const r = Diff3.merge(a, o, b);
117117
const result = r.result;
118118
// [
119119
// 'AA',
120-
// '\n<<<<<<<<<\n',
120+
// '<<<<<<<',
121121
// 'a',
122122
// 'b',
123123
// 'c',
124-
// '\n=========\n',
124+
// '=======',
125125
// 'a',
126126
// 'd',
127127
// 'c',
128-
// '\n>>>>>>>>>\n',
128+
// '>>>>>>>',
129129
// 'ZZ',
130-
// '\n<<<<<<<<<\n',
130+
// '<<<<<<<',
131131
// 'new',
132132
// '00',
133133
// 'a',
134134
// 'a',
135-
// '\n=========\n',
135+
// '=======',
136136
// '11',
137-
// '\n>>>>>>>>>\n',
137+
// '>>>>>>>',
138138
// 'M',
139139
// 'z',
140140
// 'z',

index.mjs

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,9 @@ function diff3Merge(a, o, b, options) {
332332
};
333333
options = Object.assign(defaults, options);
334334

335-
const aString = (typeof a === 'string');
336-
const oString = (typeof o === 'string');
337-
const bString = (typeof b === 'string');
338-
339-
if (aString) a = a.split(options.stringSeparator);
340-
if (oString) o = o.split(options.stringSeparator);
341-
if (bString) b = b.split(options.stringSeparator);
335+
if (typeof a === 'string') a = a.split(options.stringSeparator);
336+
if (typeof o === 'string') o = o.split(options.stringSeparator);
337+
if (typeof b === 'string') b = b.split(options.stringSeparator);
342338

343339
let results = [];
344340
const regions = diff3MergeRegions(a, o, b);
@@ -385,104 +381,128 @@ function diff3Merge(a, o, b, options) {
385381
return results;
386382
}
387383

384+
388385
function mergeDiff3(a, o, b, options) {
389-
let defaults = {
386+
const defaults = {
390387
excludeFalseConflicts: true,
391388
stringSeparator: /\s+/,
392389
label: {}
393390
};
394391
options = Object.assign(defaults, options);
395392

396-
const mergeResult = diff3Merge(a, o, b, options);
393+
const aSection = '<<<<<<<' + (options.label.a ? ` ${options.label.a}` : '');
394+
const oSection = '|||||||' + (options.label.o ? ` ${options.label.o}` : '');
395+
const xSection = '=======';
396+
const bSection = '>>>>>>>' + (options.label.b ? ` ${options.label.b}` : '');
397397

398+
const regions = diff3Merge(a, o, b, options);
398399
let conflict = false;
399-
let lines = [];
400+
let result = [];
400401

401-
mergeResult.forEach(result => {
402-
if (result.ok) {
403-
lines = lines.concat(result.ok);
404-
} else if (result.conflict) {
402+
regions.forEach(region => {
403+
if (region.ok) {
404+
result = result.concat(region.ok);
405+
} else if (region.conflict) {
405406
conflict = true;
406-
lines.push(`<<<<<<<${options.label.a ? ` ${options.label.a}` : ''}`);
407-
lines = lines.concat(result.conflict.a);
408-
lines.push(`|||||||${options.label.o ? ` ${options.label.o}` : ''}`);
409-
lines = lines.concat(result.conflict.o);
410-
lines.push('=======');
411-
lines = lines.concat(result.conflict.b);
412-
lines.push(`>>>>>>>${options.label.b ? ` ${options.label.b}` : ''}`);
407+
result = result.concat(
408+
[aSection],
409+
region.conflict.a,
410+
[oSection],
411+
region.conflict.o,
412+
[xSection],
413+
region.conflict.b,
414+
[bSection]
415+
);
413416
}
414417
});
415418

416419
return {
417420
conflict: conflict,
418-
result: lines
421+
result: result
419422
};
420423
}
421424

425+
422426
function merge(a, o, b, options) {
423-
let defaults = {
427+
const defaults = {
424428
excludeFalseConflicts: true,
425-
stringSeparator: /\s+/
429+
stringSeparator: /\s+/,
430+
label: {}
426431
};
427432
options = Object.assign(defaults, options);
428433

429-
const merger = diff3Merge(a, o, b, options);
434+
const aSection = '<<<<<<<' + (options.label.a ? ` ${options.label.a}` : '');
435+
const xSection = '=======';
436+
const bSection = '>>>>>>>' + (options.label.b ? ` ${options.label.b}` : '');
437+
438+
const regions = diff3Merge(a, o, b, options);
430439
let conflict = false;
431-
let lines = [];
432-
for (let i = 0; i < merger.length; i++) {
433-
const item = merger[i];
434-
if (item.ok) {
435-
lines = lines.concat(item.ok);
436-
} else {
440+
let result = [];
441+
442+
regions.forEach(region => {
443+
if (region.ok) {
444+
result = result.concat(region.ok);
445+
} else if (region.conflict) {
437446
conflict = true;
438-
lines = lines.concat(
439-
['\n<<<<<<<<<\n'], item.conflict.a,
440-
['\n=========\n'], item.conflict.b,
441-
['\n>>>>>>>>>\n']
447+
result = result.concat(
448+
[aSection],
449+
region.conflict.a,
450+
[xSection],
451+
region.conflict.b,
452+
[bSection]
442453
);
443454
}
444-
}
455+
});
456+
445457
return {
446458
conflict: conflict,
447-
result: lines
459+
result: result
448460
};
449461
}
450462

451463

452464
function mergeDigIn(a, o, b, options) {
453-
let defaults = {
454-
excludeFalseConflicts: false,
455-
stringSeparator: /\s+/
465+
const defaults = {
466+
excludeFalseConflicts: true,
467+
stringSeparator: /\s+/,
468+
label: {}
456469
};
457470
options = Object.assign(defaults, options);
458471

459-
const merger = diff3Merge(a, o, b, options);
472+
const aSection = '<<<<<<<' + (options.label.a ? ` ${options.label.a}` : '');
473+
const xSection = '=======';
474+
const bSection = '>>>>>>>' + (options.label.b ? ` ${options.label.b}` : '');
475+
476+
const regions = diff3Merge(a, o, b, options);
460477
let conflict = false;
461-
let lines = [];
462-
for (let i = 0; i < merger.length; i++) {
463-
const item = merger[i];
464-
if (item.ok) {
465-
lines = lines.concat(item.ok);
478+
let result = [];
479+
480+
regions.forEach(region => {
481+
if (region.ok) {
482+
result = result.concat(region.ok);
466483
} else {
467-
const c = diffComm(item.conflict.a, item.conflict.b);
484+
const c = diffComm(region.conflict.a, region.conflict.b);
468485
for (let j = 0; j < c.length; j++) {
469486
let inner = c[j];
470487
if (inner.common) {
471-
lines = lines.concat(inner.common);
488+
result = result.concat(inner.common);
472489
} else {
473490
conflict = true;
474-
lines = lines.concat(
475-
['\n<<<<<<<<<\n'], inner.buffer1,
476-
['\n=========\n'], inner.buffer2,
477-
['\n>>>>>>>>>\n']
491+
result = result.concat(
492+
[aSection],
493+
inner.buffer1,
494+
[xSection],
495+
inner.buffer2,
496+
[bSection]
478497
);
479498
}
480499
}
481500
}
482-
}
501+
});
502+
483503
return {
484504
conflict: conflict,
485-
result: lines
505+
result: result
486506
};
487507
}
488508

test/merge.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ test('merge', t => {
2222
const b = ['AA', 'a', 'd', 'c', 'ZZ', '11', 'M', 'z', 'z', '99'];
2323
const expected = [
2424
'AA',
25-
'\n<<<<<<<<<\n',
25+
'<<<<<<<',
2626
'a',
2727
'b',
2828
'c',
29-
'\n=========\n',
29+
'=======',
3030
'a',
3131
'd',
3232
'c',
33-
'\n>>>>>>>>>\n',
33+
'>>>>>>>',
3434
'ZZ',
35-
'\n<<<<<<<<<\n',
35+
'<<<<<<<',
3636
'new',
3737
'00',
3838
'a',
3939
'a',
40-
'\n=========\n',
40+
'=======',
4141
'11',
42-
'\n>>>>>>>>>\n',
42+
'>>>>>>>',
4343
'M',
4444
'z',
4545
'z',
@@ -61,13 +61,13 @@ description: "description changed"`;
6161
const b = `title: "title changed"
6262
description: "description"`;
6363
const expected = [
64-
'\n<<<<<<<<<\n',
64+
'<<<<<<<',
6565
'title: "title"',
6666
'description: "description changed"',
67-
'\n=========\n',
67+
'=======',
6868
'title: "title changed"',
6969
'description: "description"',
70-
'\n>>>>>>>>>\n'
70+
'>>>>>>>'
7171
];
7272

7373
const r = Diff3.merge(a, o, b, { stringSeparator: /[\r\n]+/ });

test/mergeDigIn.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ test('mergeDigIn', t => {
2323
const expected = [
2424
'AA',
2525
'a',
26-
'\n<<<<<<<<<\n',
26+
'<<<<<<<',
2727
'b',
28-
'\n=========\n',
28+
'=======',
2929
'd',
30-
'\n>>>>>>>>>\n',
30+
'>>>>>>>',
3131
'c',
3232
'ZZ',
33-
'\n<<<<<<<<<\n',
33+
'<<<<<<<',
3434
'new',
3535
'00',
3636
'a',
3737
'a',
38-
'\n=========\n',
38+
'=======',
3939
'11',
40-
'\n>>>>>>>>>\n',
40+
'>>>>>>>',
4141
'M',
4242
'z',
4343
'z',

0 commit comments

Comments
 (0)