New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

array-changes

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-changes - npm Package Compare versions

Comparing version 2.0.0 to 3.0.0

80

lib/arrayChanges.js

@@ -13,3 +13,17 @@ var arrayDiff = require('arraydiff-papandreou');

module.exports = function arrayChanges(actual, expected, equal, similar, includeNonNumericalProperties) {
module.exports = function arrayChanges(actual, expected, equal, similar, options) {
if (Array.isArray(options) || typeof options === 'boolean') {
throw new Error([
'It looks like you are using arrayChanges includeNonNumericalProperties,',
'the API has changed. You need to supply it as an entry to an option object:',
'arrayChanges(actual, expected, equal, similar, {',
' includeNonNumericalProperties: ["foo", "bar"]',
'})'
].join('\n'));
}
options = options || {};
var includeNonNumericalProperties = options.includeNonNumericalProperties;
var fallbackToItemByItemDiff = 'fallbackToItemByItemDiff' in options ? options.fallbackToItemByItemDiff : true;
var mutatedArray = new Array(actual.length);

@@ -113,39 +127,41 @@

var c, i;
for (i = 0, c = 0; i < Math.max(actual.length, expected.length) && c <= conflicts; i += 1) {
if (
i >= actual.length || i >= expected.length || (!equal(actual[i], expected[i], i, i) && !similar(actual[i], expected[i], i, i))
) {
c += 1;
if (fallbackToItemByItemDiff) {
var c, i;
for (i = 0, c = 0; i < Math.max(actual.length, expected.length) && c <= conflicts; i += 1) {
if (
i >= actual.length || i >= expected.length || (!equal(actual[i], expected[i], i, i) && !similar(actual[i], expected[i], i, i))
) {
c += 1;
}
}
}
if (c <= conflicts) {
mutatedArray = [];
var j;
for (j = 0; j < Math.min(actual.length, expected.length); j += 1) {
mutatedArray.push({
type: 'similar',
value: actual[j],
expected: expected[j],
actualIndex: j,
expectedIndex: j
});
}
if (actual.length < expected.length) {
for (; j < Math.max(actual.length, expected.length); j += 1) {
if (c <= conflicts) {
mutatedArray = [];
var j;
for (j = 0; j < Math.min(actual.length, expected.length); j += 1) {
mutatedArray.push({
type: 'insert',
value: expected[j],
type: 'similar',
value: actual[j],
expected: expected[j],
actualIndex: j,
expectedIndex: j
});
}
} else {
for (; j < Math.max(actual.length, expected.length); j += 1) {
mutatedArray.push({
type: 'remove',
value: actual[j],
actualIndex: j
});
if (actual.length < expected.length) {
for (; j < Math.max(actual.length, expected.length); j += 1) {
mutatedArray.push({
type: 'insert',
value: expected[j],
expectedIndex: j
});
}
} else {
for (; j < Math.max(actual.length, expected.length); j += 1) {
mutatedArray.push({
type: 'remove',
value: actual[j],
actualIndex: j
});
}
}

@@ -152,0 +168,0 @@ }

6

package.json
{
"name": "array-changes",
"version": "2.0.0",
"version": "3.0.0",
"description": "Array diffing",

@@ -34,4 +34,4 @@ "main": "./lib/arrayChanges.js",

"coveralls": "2.11.6",
"eslint": "2.7.0",
"eslint-config-onelint": "1.0.2",
"eslint": "2.13.1",
"eslint-config-onelint": "1.2.0",
"istanbul": "0.4.2",

@@ -38,0 +38,0 @@ "mocha": "2.3.4",

@@ -244,54 +244,39 @@ /*global describe, it, 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 = {};
var b = [1, 2, 3];
b.bar = 456;
b.baz = 789;
b.quux = false;
expect(arrayChanges(a, b, function (a, b) {
it('falls back to item by item diffing if that results in less conflicts', function () {
expect(arrayChanges([1, 2, 5], [1, 3, 4], function (a, b) {
return a === b;
}, function (a, b) {
return a === b;
}, true), 'to equal', [
}), '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 }
{ type: 'similar', value: 2, expected: 3, actualIndex: 1, expectedIndex: 1 },
{ type: 'similar', value: 5, expected: 4, actualIndex: 2, expectedIndex: 2, 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;
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 }
]);
describe('when item by item diff is disabled', function () {
it('does not use the item by item diff even if that would result in less conflicts', function () {
expect(arrayChanges([1, 2, 5], [1, 3, 4], function (a, b) {
return a === b;
}, function () {
return false;
}, { fallbackToItemByItemDiff: false }), 'to equal', [
{ type: 'equal', value: 1, actualIndex: 0, expected: 1, expectedIndex: 0 },
{ type: 'insert', value: 3, expectedIndex: 1 },
{ type: 'insert', value: 4, expectedIndex: 1 },
{ type: 'remove', value: 2, actualIndex: 1 },
{ type: 'remove', value: 5, actualIndex: 2, last: true }
]);
});
});
if (typeof Symbol !== 'undefined') {
it('should diff arrays that have Symbol property names', function () {
var aSymbol = Symbol('a');
var bSymbol = Symbol('b');
var a = [1, 2];
a[aSymbol] = 123;
describe('when including non-numerical properties', function () {
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];
b[bSymbol] = 456;
var b = [1, 2, 3];
b.bar = 456;
b.baz = 789;
b.quux = false;
expect(arrayChanges(a, b, function (a, b) {

@@ -301,11 +286,54 @@ return a === b;

return a === b;
}, true), 'to equal', [
}, { includeNonNumericalProperties: true }), 'to equal', [
{ type: 'equal', value: 1, expected: 1, actualIndex: 0, expectedIndex: 0 },
{ type: 'equal', value: 2, expected: 2, actualIndex: 1, expectedIndex: 1 },
{ type: 'remove', value: 123, actualIndex: aSymbol },
{ type: 'insert', value: 456, expectedIndex: bSymbol, last: true }
{ 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;
expect(arrayChanges(a, b, function (a, b) {
return a === b;
}, function (a, b) {
return a === b;
}, { includeNonNumericalProperties: [ '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');
var bSymbol = Symbol('b');
var a = [1, 2];
a[aSymbol] = 123;
var b = [1, 2];
b[bSymbol] = 456;
expect(arrayChanges(a, b, function (a, b) {
return a === b;
}, function (a, b) {
return a === b;
}, { includeNonNumericalProperties: true }), 'to equal', [
{ type: 'equal', value: 1, expected: 1, actualIndex: 0, expectedIndex: 0 },
{ type: 'equal', value: 2, expected: 2, actualIndex: 1, expectedIndex: 1 },
{ type: 'remove', value: 123, actualIndex: aSymbol },
{ type: 'insert', value: 456, expectedIndex: bSymbol, last: true }
]);
});
}
});
it('produces a valid plan', function () {

@@ -312,0 +340,0 @@ this.timeout(20000);

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc