Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.

Commit 9e24284

Browse files
pedrottimarkkpdecker
authored andcommitted
Fix: diffArrays can compare falsey items
1 parent 00e2f94 commit 9e24284

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/diff/array.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ export const arrayDiff = new Diff();
44
arrayDiff.tokenize = arrayDiff.join = function(value) {
55
return value.slice();
66
};
7+
arrayDiff.removeEmpty = function(value) {
8+
return value;
9+
};
710

811
export function diffArrays(oldArr, newArr, callback) { return arrayDiff.diff(oldArr, newArr, callback); }

test/diff/array.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,23 @@ describe('diff/array', function() {
1515
{count: 1, value: [c], removed: true, added: undefined}
1616
]);
1717
});
18+
it('should diff falsey values', function() {
19+
const a = false;
20+
const b = 0;
21+
const c = '';
22+
// Example sequences from Myers 1986
23+
const arrayA = [c, b, a, b, a, c];
24+
const arrayB = [a, b, c, a, b, b, a];
25+
const diffResult = diffArrays(arrayA, arrayB);
26+
expect(diffResult).to.deep.equals([
27+
{count: 2, value: [a, b], removed: undefined, added: true},
28+
{count: 1, value: [c]},
29+
{count: 1, value: [b], removed: true, added: undefined},
30+
{count: 2, value: [a, b]},
31+
{count: 1, value: [b], removed: undefined, added: true},
32+
{count: 1, value: [a]},
33+
{count: 1, value: [c], removed: true, added: undefined}
34+
]);
35+
});
1836
});
1937
});

0 commit comments

Comments
 (0)