From 5b7a31260bd6411f8299aa25ced79338897beecb Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Fri, 29 Sep 2017 16:56:31 -0400 Subject: [PATCH] Fix: diffArrays can compare falsey items --- src/diff/array.js | 3 +++ test/diff/array.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/diff/array.js b/src/diff/array.js index 4c2a3292..28de3a0c 100644 --- a/src/diff/array.js +++ b/src/diff/array.js @@ -4,5 +4,8 @@ export const arrayDiff = new Diff(); arrayDiff.tokenize = arrayDiff.join = function(value) { return value.slice(); }; +arrayDiff.removeEmpty = function(value) { + return value; +}; export function diffArrays(oldArr, newArr, callback) { return arrayDiff.diff(oldArr, newArr, callback); } diff --git a/test/diff/array.js b/test/diff/array.js index 1dde77bc..2fd6893a 100644 --- a/test/diff/array.js +++ b/test/diff/array.js @@ -15,5 +15,23 @@ describe('diff/array', function() { {count: 1, value: [c], removed: true, added: undefined} ]); }); + it('should diff falsey values', function() { + const a = false; + const b = 0; + const c = ''; + // Example sequences from Myers 1986 + const arrayA = [c, b, a, b, a, c]; + const arrayB = [a, b, c, a, b, b, a]; + const diffResult = diffArrays(arrayA, arrayB); + expect(diffResult).to.deep.equals([ + {count: 2, value: [a, b], removed: undefined, added: true}, + {count: 1, value: [c]}, + {count: 1, value: [b], removed: true, added: undefined}, + {count: 2, value: [a, b]}, + {count: 1, value: [b], removed: undefined, added: true}, + {count: 1, value: [a]}, + {count: 1, value: [c], removed: true, added: undefined} + ]); + }); }); });