array-changes-async
Advanced tools
Comparing version 2.2.0 to 2.2.1
@@ -45,5 +45,12 @@ /*global setTimeout */ | ||
var removeTable = []; | ||
function offsetIndex(index) { | ||
return index + (removeTable[index - 1] || 0); | ||
var offsetIndex = 0; | ||
var i; | ||
for (i = 0; i < mutatedArray.length && offsetIndex < index; i += 1) { | ||
if (mutatedArray[i].type !== 'remove') { | ||
offsetIndex++; | ||
} | ||
} | ||
return i; | ||
} | ||
@@ -55,3 +62,2 @@ | ||
var removesByIndex = {}; | ||
var removedItems = 0; | ||
@@ -64,15 +70,4 @@ removes.forEach(function (diffItem) { | ||
removedItems += diffItem.howMany; | ||
removesByIndex[diffItem.index] = removedItems; | ||
}); | ||
function updateRemoveTable() { | ||
removedItems = 0; | ||
Array.prototype.forEach.call(actual, function (_, index) { | ||
removedItems += removesByIndex[index] || 0; | ||
removeTable[index] = removedItems; | ||
}); | ||
} | ||
updateRemoveTable(); | ||
var moves = itemsDiff.filter(function (diffItem) { | ||
@@ -82,5 +77,4 @@ return diffItem.type === 'move'; | ||
var movedItems = 0; | ||
moves.forEach(function (diffItem) { | ||
var moveFromIndex = offsetIndex(diffItem.from); | ||
var moveFromIndex = offsetIndex(diffItem.from + 1) - 1; | ||
var removed = mutatedArray.slice(moveFromIndex, diffItem.howMany + moveFromIndex); | ||
@@ -93,6 +87,4 @@ var added = removed.map(function (v) { | ||
}); | ||
Array.prototype.splice.apply(mutatedArray, [offsetIndex(diffItem.to), 0].concat(added)); | ||
movedItems += diffItem.howMany; | ||
removesByIndex[diffItem.from] = movedItems; | ||
updateRemoveTable(); | ||
var insertIndex = offsetIndex(diffItem.to); | ||
Array.prototype.splice.apply(mutatedArray, [insertIndex, 0].concat(added)); | ||
}); | ||
@@ -99,0 +91,0 @@ |
{ | ||
"name": "array-changes-async", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Array diffing", | ||
@@ -32,2 +32,3 @@ "main": "./lib/arrayChanges.js", | ||
"bundle-collapser": "1.2.0", | ||
"chance-generators": "1.18.0", | ||
"coveralls": "2.11.2", | ||
@@ -37,4 +38,5 @@ "istanbul": "0.3.17", | ||
"mocha": "2.2.5", | ||
"unexpected": "10.1.0" | ||
"unexpected": "10.1.0", | ||
"unexpected-check": "1.11.0" | ||
} | ||
} |
/*global describe, it, setTimeout, Symbol*/ | ||
var arrayChanges = require('../lib/arrayChanges'); | ||
var expect = require('unexpected'); | ||
var expect = require('unexpected').clone() | ||
.use(require('unexpected-check')); | ||
var generators = require('chance-generators'); | ||
@@ -8,2 +10,3 @@ function toArguments() { | ||
} | ||
function promiseArrayChanges(actual, expected, equal, similar, includeNonNumericalProperties) { | ||
@@ -35,2 +38,39 @@ var isCalled = 0; | ||
function executeDiff(changes) { | ||
var result = []; | ||
changes.forEach(function (item) { | ||
switch (item.type) { | ||
case 'moveTarget': | ||
case 'insert': | ||
result.push(item.value); | ||
break; | ||
case 'equal': | ||
case 'similar': | ||
if (typeof item.expected === 'number') { | ||
result.push(item.expected); | ||
} | ||
break; | ||
} | ||
}); | ||
return result; | ||
} | ||
expect.addAssertion('<array> when diffed with <array> <assertion>', function (expect, actual, expected) { | ||
expect.errorMode = 'nested'; | ||
return promiseArrayChanges(actual, expected, function (a, b, aIndex, bIndex, callback) { | ||
return callback(a === b); | ||
}).then(function (diff) { | ||
return expect.shift(diff); | ||
}); | ||
}); | ||
expect.addAssertion('<array> when executing the diff <assertion>', function (expect, diff) { | ||
expect.errorMode = 'nested'; | ||
return expect.shift(executeDiff(diff)); | ||
}); | ||
describe('array-changes-async', function () { | ||
@@ -359,3 +399,19 @@ it('returns an empty change-list when the two arrays are both empty', function () { | ||
} | ||
it('produces a valid plan', function () { | ||
var g = generators(42); | ||
var arrays = g.array(g.natural({ max: 10 }), g.natural({ max: 10 })); | ||
return expect(function (actual, expected) { | ||
return expect( | ||
actual, | ||
'when diffed with', | ||
expected, | ||
'when executing the diff', | ||
'to equal', | ||
expected | ||
); | ||
}, 'to be valid for all', arrays, arrays); | ||
}); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
626
37402
9
11
1