Comparing version 12.2.9 to 12.3.0
# Changelog | ||
## 12.3.0 | ||
add | ||
* deepDifference | ||
## 12.2.0 | ||
@@ -4,0 +9,0 @@ |
150
deep.js
@@ -8,2 +8,3 @@ export { | ||
deepEqualAdded, | ||
deepDifference, | ||
}; | ||
@@ -351,2 +352,151 @@ | ||
/** | ||
* @function deepDifference finds the differences betwenn two objects | ||
* @param {*} obj1 The first object | ||
* @param {*} obj2 The second object | ||
* @var {Object} deepDifferences contains the differences results | ||
* @return {deepDifferences} returns an Object of differences | ||
*/ | ||
const deepDifference = function(obj1, obj2) { | ||
if (!obj2 || typeof obj2 !== `object`) { | ||
return obj1; | ||
} | ||
const deepDifferences = { | ||
additions: [], | ||
removals: [], | ||
changes: [], | ||
}; | ||
/** | ||
* @function compare two items and push non-matches to object | ||
* @param {*} item1 The first item | ||
* @param {*} item2 The second item | ||
* @param {String} key The key in our object | ||
* | ||
* Grab the object types for better comparison, since arrays by default return type object | ||
* @var {*} type1 the object type for the first item | ||
* @var {*} type2 the object type for the first item | ||
*/ | ||
const compare = function(item1, item2, key) { | ||
const type1 = Object.prototype.toString.call(item1); | ||
const type2 = Object.prototype.toString.call(item2); | ||
//if item2 has undefined type, assign null to its value | ||
if (type2 === `[object Undefined]`) { | ||
const nameArray = []; | ||
nameArray.push(key); | ||
const changed = { name: nameArray, oldValue: item1, newValue: null }; | ||
deepDifferences.changes.push(changed); | ||
return; | ||
} | ||
//if object call deepDifference recursively | ||
if (type1 === `[object Object]`) { | ||
const nameArray = []; | ||
nameArray.push(key); | ||
const objdeepDifference = deepDifference(item1, item2); | ||
if (objdeepDifference.additions.length > 0) { | ||
nameArray.push(objdeepDifference.additions[0].name[0]); | ||
const added = { | ||
name: nameArray, | ||
value: objdeepDifference.additions[0].value, | ||
}; | ||
deepDifferences.additions.push(added); | ||
} | ||
if (objdeepDifference.removals.length > 0) { | ||
nameArray.push(objdeepDifference.removals[0].name[0]); | ||
const removed = { | ||
name: nameArray, | ||
value: objdeepDifference.removals[0].value, | ||
}; | ||
deepDifferences.removals.push(removed); | ||
} | ||
return; | ||
} | ||
//if function. convert to string and compare | ||
if (type1 === `[object Function]`) { | ||
if (item1.toString() !== item2.toString()) { | ||
const nameArray = []; | ||
nameArray.push(key); | ||
const changed = { name: nameArray, oldValue: item1, newValue: item2 }; | ||
deepDifferences.changes.push(changed); | ||
} | ||
} | ||
if (type1 === `[object Array]`) { | ||
if (!arraysMatch(item1, item2)) { | ||
const nameArray = []; | ||
nameArray.push(key); | ||
const changed = { name: nameArray, oldValue: item1, newValue: item2 }; | ||
deepDifferences.changes.push(changed); | ||
} | ||
return; | ||
} | ||
if (item1 !== item2) { | ||
const nameArray = []; | ||
nameArray.push(key); | ||
const changed = { name: nameArray, oldValue: item1, newValue: item2 }; | ||
deepDifferences.changes.push(changed); | ||
return; | ||
} | ||
if (type1 !== type2) { | ||
const nameArray = []; | ||
nameArray.push(key); | ||
const changed = { name: nameArray, oldValue: item1, newValue: item2 }; | ||
deepDifferences.changes.push(changed); | ||
return; | ||
} | ||
}; | ||
Object.keys(obj1).forEach(key => { | ||
/** | ||
* If obj2 is missing a property in obj1 | ||
* add property to removals array | ||
**/ | ||
if (!{}.hasOwnProperty.call(obj2, key)) { | ||
const nameArray = []; | ||
nameArray.push(key); | ||
const removed = { name: nameArray, value: obj1[key] }; | ||
deepDifferences.removals.push(removed); | ||
return; | ||
} | ||
compare(obj1[key], obj2[key], key); | ||
}); | ||
Object.keys(obj2).forEach(key => { | ||
if (!{}.hasOwnProperty.call(obj1, key)) { | ||
const nameArray = []; | ||
nameArray.push(key); | ||
const added = { name: nameArray, value: obj2[key] }; | ||
deepDifferences.additions.push(added); | ||
return; | ||
} | ||
}); | ||
return deepDifferences; | ||
}; | ||
const arraysMatch = function(arr1, arr2) { | ||
// Check if the arrays are the same length | ||
if (arr1.length !== arr2.length) { | ||
return false; | ||
} | ||
// Check if all items exist and are in the same order | ||
for (let i = 0; i < arr1.length; i = +1) { | ||
if (arr1[i] !== arr2[i]) { | ||
return false; | ||
} | ||
} | ||
// Otherwise, return true | ||
return true; | ||
}; | ||
const isObject = x => { | ||
@@ -353,0 +503,0 @@ return typeof x === `object` && x !== null; |
{ | ||
"name": "utilsac", | ||
"version": "12.2.9", | ||
"version": "12.3.0", | ||
"description": "Utility functions", | ||
@@ -15,3 +15,3 @@ "license": "CC0-1.0", | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"ava": "^3.4.0", | ||
"eslint": "^6.5.1", | ||
@@ -31,3 +31,3 @@ "eslint-config-red": "^1.4.1", | ||
"env": { | ||
"es2020": true, | ||
"es2020": true, | ||
"browser": true | ||
@@ -37,21 +37,8 @@ } | ||
"ava": { | ||
"nodeArguments": [ | ||
"--experimental-modules" | ||
], | ||
"files": [ | ||
"tests/specification/**" | ||
], | ||
"require": [ | ||
"esm" | ||
], | ||
"babel": { | ||
"testOptions": { | ||
"presets": [ | ||
[ | ||
"module:ava/stage-4", | ||
{ | ||
"modules": false | ||
} | ||
] | ||
] | ||
} | ||
}, | ||
"compileEnhancements": false | ||
] | ||
}, | ||
@@ -58,0 +45,0 @@ "files": [ |
@@ -39,2 +39,3 @@ # Utility functions | ||
deepEqualAdded, | ||
deepDifference, | ||
} from "utilsac/deep.js"; | ||
@@ -41,0 +42,0 @@ ``` |
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
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
35913
754
116