Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: diffArrays can compare falsey items #197

Merged
merged 1 commit into from Oct 7, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/diff/array.js
Expand Up @@ -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); }
18 changes: 18 additions & 0 deletions test/diff/array.js
Expand Up @@ -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}
]);
});
});
});