diff --git a/README.md b/README.md index e02b856..3513857 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/diff3.mjs b/src/diff3.mjs index dbc3a0b..f762a1e 100644 --- a/src/diff3.mjs +++ b/src/diff3.mjs @@ -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]); } } @@ -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; diff --git a/test/LCS.test.js b/test/LCS.test.js index 5ecc294..3da1ba3 100644 --- a/test/LCS.test.js +++ b/test/LCS.test.js @@ -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. diff --git a/test/diff3Merge.test.js b/test/diff3Merge.test.js index a22231c..0cb8735 100644 --- a/test/diff3Merge.test.js +++ b/test/diff3Merge.test.js @@ -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}`);