Skip to content

Commit

Permalink
Allow passing an array of the non-numerical keys to add to the diff.
Browse files Browse the repository at this point in the history
This is in order to support a custom getKeys implementation that might not
correspond to what Object.keys says.
  • Loading branch information
papandreou committed Feb 17, 2016
1 parent 22f9319 commit 90a6f18
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/arrayChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,26 @@ module.exports = function arrayChanges(actual, expected, equal, similar, include
if (includeNonNumericalProperties) {
var isSeenByNonNumericalKey = {};
var nonNumericalKeys = [];
[actual, expected].forEach(function (obj) {
Object.keys(obj).forEach(function (key) {
if (!/^(?:0|[1-9][0-9]*)$/.test(key) && !isSeenByNonNumericalKey[key]) {
isSeenByNonNumericalKey[key] = true;
nonNumericalKeys.push(key);
}
});
if (Object.getOwnPropertySymbols) {
Object.getOwnPropertySymbols(obj).forEach(function (symbol) {
if (!isSeenByNonNumericalKey[symbol]) {
isSeenByNonNumericalKey[symbol] = true;
nonNumericalKeys.push(symbol);
if (Array.isArray(includeNonNumericalProperties)) {
nonNumericalKeys = includeNonNumericalProperties;
} else {
[actual, expected].forEach(function (obj) {
Object.keys(obj).forEach(function (key) {
if (!/^(?:0|[1-9][0-9]*)$/.test(key) && !isSeenByNonNumericalKey[key]) {
isSeenByNonNumericalKey[key] = true;
nonNumericalKeys.push(key);
}
});
}
});
if (Object.getOwnPropertySymbols) {
Object.getOwnPropertySymbols(obj).forEach(function (symbol) {
if (!isSeenByNonNumericalKey[symbol]) {
isSeenByNonNumericalKey[symbol] = true;
nonNumericalKeys.push(symbol);
}
});
}
});
}
nonNumericalKeys.forEach(function (key) {
if (key in actual) {
if (key in expected) {
Expand Down
18 changes: 18 additions & 0 deletions test/arrayChanges.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@ describe('array-changes', function () {
]);
});

it('should support an array of specific non-numerical keys to diff', function () {
var a = [1];
a.foo = 123;
a.bar = 789;

var b = [1];
a.foo = 456;
a.bar = false;
expect(arrayChanges(a, b, function (a, b) {
return a === b;
}, function (a, b) {
return a === b;
}, [ 'foo' ]), 'to equal', [
{ type: 'equal', value: 1, expected: 1, actualIndex: 0, expectedIndex: 0 },
{ type: 'remove', actualIndex: 'foo', value: 456, last: true }
]);
});

if (typeof Symbol !== 'undefined') {
it('should diff arrays that have Symbol property names', function () {
var aSymbol = Symbol('a');
Expand Down

0 comments on commit 90a6f18

Please sign in to comment.