array-changes-async
Advanced tools
Comparing version
@@ -46,7 +46,7 @@ /*global setTimeout */ | ||
function offsetIndex(index) { | ||
var offsetIndex = 0; | ||
var currentOffsetIndex = 0; | ||
var i; | ||
for (i = 0; i < mutatedArray.length && offsetIndex < index; i += 1) { | ||
for (i = 0; i < mutatedArray.length && currentOffsetIndex < index; i += 1) { | ||
if (mutatedArray[i].type !== 'remove' && mutatedArray[i].type !== 'moveSource') { | ||
offsetIndex++; | ||
currentOffsetIndex += 1; | ||
} | ||
@@ -235,4 +235,11 @@ } | ||
nonNumericalKeys.forEach(function (key) { | ||
var valueExpected; | ||
if (key in actual) { | ||
if (key in expected) { | ||
var valueActual = actual[key]; | ||
valueExpected = expected[key]; | ||
if (key in expected && typeof valueExpected !== 'undefined') { | ||
valueExpected = expected[key]; | ||
mutatedArray.push({ | ||
@@ -242,18 +249,22 @@ type: 'similar', | ||
actualIndex: key, | ||
value: actual[key], | ||
expected: expected[key] | ||
value: valueActual, | ||
expected: valueExpected | ||
}); | ||
} else { | ||
} else if (typeof valueActual !== 'undefined') { | ||
mutatedArray.push({ | ||
type: 'remove', | ||
actualIndex: key, | ||
value: actual[key] | ||
value: valueActual | ||
}); | ||
} | ||
} else { | ||
mutatedArray.push({ | ||
type: 'insert', | ||
expectedIndex: key, | ||
value: expected[key] | ||
}); | ||
valueExpected = expected[key]; | ||
if (typeof valueExpected !== 'undefined') { | ||
mutatedArray.push({ | ||
type: 'insert', | ||
expectedIndex: key, | ||
value: valueExpected | ||
}); | ||
} | ||
} | ||
@@ -260,0 +271,0 @@ }); |
{ | ||
"name": "array-changes-async", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Array diffing", | ||
@@ -5,0 +5,0 @@ "main": "./lib/arrayChanges.js", |
@@ -307,43 +307,102 @@ /*global describe, it, setTimeout, Symbol*/ | ||
it('should diff arrays that have non-numerical property names', function () { | ||
var a = [1, 2, 3]; | ||
a.foo = 123; | ||
a.bar = 456; | ||
a.quux = {}; | ||
describe('when including non-numerical properties', function () { | ||
it('returns an empty change-list with an undefined key on the LHS', function () { | ||
var a = []; | ||
a.nothing = undefined; | ||
var b = []; | ||
var b = [1, 2, 3]; | ||
b.bar = 456; | ||
b.baz = 789; | ||
b.quux = false; | ||
return expect(promiseArrayChanges(a, b, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, true), 'when fulfilled', 'to equal', [ | ||
{ type: 'equal', value: 1, expected: 1, actualIndex: 0, expectedIndex: 0 }, | ||
{ type: 'equal', value: 2, expected: 2, actualIndex: 1, expectedIndex: 1 }, | ||
{ type: 'equal', value: 3, expected: 3, actualIndex: 2, expectedIndex: 2 }, | ||
{ type: 'remove', value: 123, actualIndex: 'foo' }, | ||
{ type: 'equal', value: 456, expected: 456, actualIndex: 'bar', expectedIndex: 'bar' }, | ||
{ type: 'similar', value: {}, expected: false, actualIndex: 'quux', expectedIndex: 'quux' }, | ||
{ type: 'insert', value: 789, expectedIndex: 'baz', last: true } | ||
]); | ||
}); | ||
return expect(promiseArrayChanges(a, b, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, undefined, true), 'when fulfilled', 'to equal', []); | ||
}); | ||
it('should support an array of specific non-numerical keys to diff', function () { | ||
var a = [1]; | ||
a.foo = 123; | ||
a.bar = 789; | ||
it('returns an empty change-list with an undefined key on the RHS', function () { | ||
var a = []; | ||
var b = []; | ||
b.nothing = undefined; | ||
var b = [1]; | ||
a.foo = 456; | ||
a.bar = false; | ||
return expect(promiseArrayChanges(a, b, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, [ 'foo' ]), 'when fulfilled', 'to equal', [ | ||
{ type: 'equal', actualIndex: 0, expectedIndex: 0, value: 1, expected: 1 }, | ||
{ type: 'remove', actualIndex: 'foo', value: 456, last: true } | ||
]); | ||
return expect(promiseArrayChanges(a, b, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, undefined, true), 'when fulfilled', 'to equal', []); | ||
}); | ||
it('returns an empty change-list with undefined keys on both the LHS and RHS', function () { | ||
var a = []; | ||
a.nothing = undefined; | ||
var b = []; | ||
b.nothing = undefined; | ||
return expect(promiseArrayChanges(a, b, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, undefined, true), 'when fulfilled', 'to equal', []); | ||
}); | ||
it('returns a change-list containing remove when a LHS key is undefined on the RHS', function () { | ||
var a = []; | ||
a.nothing = true; | ||
var b = []; | ||
b.nothing = undefined; | ||
return expect(promiseArrayChanges(a, b, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, undefined, true), 'when fulfilled', 'to equal', [ | ||
{ type: 'remove', actualIndex: 'nothing', value: true, expected: undefined, last: true } | ||
]); | ||
}); | ||
it('returns a change-list containing similar when a RHS key is undefined on the LHS', function () { | ||
var a = []; | ||
a.nothing = undefined; | ||
var b = []; | ||
b.nothing = true; | ||
return expect(promiseArrayChanges(a, b, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, undefined, true), 'when fulfilled', 'to equal', [ | ||
{ type: 'similar', expectedIndex: 'nothing', actualIndex: 'nothing', value: undefined, expected: true, last: true } | ||
]); | ||
}); | ||
it('should diff arrays that have non-numerical property names', function () { | ||
var a = [1, 2, 3]; | ||
a.foo = 123; | ||
a.bar = 456; | ||
a.quux = {}; | ||
var b = [1, 2, 3]; | ||
b.bar = 456; | ||
b.baz = 789; | ||
b.quux = false; | ||
return expect(promiseArrayChanges(a, b, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, true), 'when fulfilled', 'to equal', [ | ||
{ type: 'equal', value: 1, expected: 1, actualIndex: 0, expectedIndex: 0 }, | ||
{ type: 'equal', value: 2, expected: 2, actualIndex: 1, expectedIndex: 1 }, | ||
{ type: 'equal', value: 3, expected: 3, actualIndex: 2, expectedIndex: 2 }, | ||
{ type: 'remove', value: 123, actualIndex: 'foo' }, | ||
{ type: 'equal', value: 456, expected: 456, actualIndex: 'bar', expectedIndex: 'bar' }, | ||
{ type: 'similar', value: {}, expected: false, actualIndex: 'quux', expectedIndex: 'quux' }, | ||
{ type: 'insert', value: 789, expectedIndex: 'baz', last: true } | ||
]); | ||
}); | ||
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; | ||
return expect(promiseArrayChanges(a, b, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, function (a, b, aIndex, bIndex, callback) { | ||
callback(a === b); | ||
}, [ 'foo' ]), 'when fulfilled', 'to equal', [ | ||
{ type: 'equal', actualIndex: 0, expectedIndex: 0, value: 1, expected: 1 }, | ||
{ type: 'remove', actualIndex: 'foo', value: 456, last: true } | ||
]); | ||
}); | ||
}); | ||
@@ -350,0 +409,0 @@ |
41021
8.04%685
8.9%