Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ See the [CONTRIBUTING.md](CONTRIBUTING.md) file for more info.

## API Reference

Buffers are arrays (or strings, which are split into arrays). Items are compared with strict equality (`===`), so objects match only themselves, and `1` does not match `'1'`.

* [3-way diff and merging](#3-way-diff-and-merging)
* [diff3Merge](#diff3Merge)
* [merge](#merge)
Expand Down
20 changes: 10 additions & 10 deletions src/diff3.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ function LCS(buffer1, buffer2) {
// updated for them would cost more than it saves.)
const SMALL_CLASS = 16;

// Use a prototype-less object so input elements that happen to match
// `Object.prototype` members (`constructor`, `__proto__`, `toString`, ...)
// don't shadow the lookup with inherited values.
let equivalenceClasses = Object.create(null);
// Use a Map so items are matched like `===` (objects by identity, `1` and `'1'`
// apart), rather than by their string form as object keys would be.
const equivalenceClasses = new Map();
let largeClasses = [];
for (let j = prefixLength; j < buffer2.length; j++) {
const item = buffer2[j];
if (equivalenceClasses[item]) {
equivalenceClasses[item].push(j);
if (equivalenceClasses[item].length === SMALL_CLASS + 1) {
largeClasses.push(equivalenceClasses[item]);
const indices = equivalenceClasses.get(item);
if (indices) {
indices.push(j);
if (indices.length === SMALL_CLASS + 1) {
largeClasses.push(indices);
}
} else {
equivalenceClasses[item] = [j];
equivalenceClasses.set(item, [j]);
}
}

Expand Down Expand Up @@ -121,7 +121,7 @@ function LCS(buffer1, buffer2) {

for (let i = prefixLength; i < buffer1.length; i++) {
const item = buffer1[i];
const indices = equivalenceClasses[item];
const indices = equivalenceClasses.get(item);
if (!indices) continue;

const start = indices.length > SMALL_CLASS ? slotStart.get(indices) : -1;
Expand Down
13 changes: 13 additions & 0 deletions test/LCS.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ describe('LCS', () => {
}
});

it('matches elements with strict equality', () => {
// Regression: elements used to be matched by their string form, so all
// objects matched each other, and `1` matched `'1'`.
const x = { x: 1 };
assert.deepEqual(Diff3.LCS([{ a: 1 }], [{ b: 2 }]).chain, null);
assert.deepEqual(Diff3.LCS([{ a: 1 }], [{ a: 1 }]).chain, null);
assert.deepEqual(Diff3.LCS([1], ['1']).chain, null);
assert.deepEqual(Diff3.LCS([null], ['null']).chain, null);

const result = Diff3.LCS([x, 1, 'a'], [x, '1', 'a']);
assert.deepEqual(lcsPairs([x, 1, 'a'], [x, '1', 'a'], result), [[0, 0], [2, 2]]);
});

it('finds a longest common subsequence when inputs have many repeated lines', () => {
// Small random inputs, mostly empty lines, checked against brute force.
// Seeded so any failure replays.
Expand Down
15 changes: 15 additions & 0 deletions test/diff3Merge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ description: "description"`;
});


it('compares objects by identity', () => {
// Regression: objects used to all match each other, so `b`'s change was lost
const x = { x: 1 };
const y = { y: 2 };
const z = { z: 3 };
const result = Diff3.diff3Merge([x, y], [x], [z]);

assert.deepEqual(result.length, 1);
assert.equal(result[0].conflict.a[0], x);
assert.equal(result[0].conflict.a[1], y);
assert.equal(result[0].conflict.o[0], x);
assert.equal(result[0].conflict.b[0], z);
});


it('handles very large inputs', () => {
// Copying a large region with `push(...items)` used to overflow the call stack
const o = Array.from({ length: 1000000 }, (_, i) => `line ${i}`);
Expand Down
Loading