Skip to content

Commit

Permalink
fix: use lodash to compare nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed May 6, 2019
1 parent d658abc commit b10733e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 39 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"standard-version": "^5.0.2"
},
"dependencies": {
"@babel/types": "^7.4.4"
"@babel/types": "^7.4.4",
"lodash": "^4.17.11"
},
"husky": {
"hooks": {
Expand Down
43 changes: 5 additions & 38 deletions src/utils/compareNodes.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,7 @@
module.exports = function compareNodes(a, b) {
if (typeof a !== typeof b) {
return false;
}
const isEqualWith = require('lodash/isEqualWith');

if (typeof a !== 'object') {
return a === b;
}

for (const key in a) {
if (a.hasOwnProperty(key)) {
// Ignore location data
if (key === 'start' || key === 'end' || key === 'loc') {
continue;
}

if (b.hasOwnProperty(key) === false) {
return false;
}

if (typeof a[key] === 'object') {
if (typeof b[key] !== 'object') {
return false;
}

if (compareNodes(a[key], b[key]) === false) {
return false;
}

continue;
}

if (a[key] !== b[key]) {
return false;
}
}
}

return true;
module.exports = (obj1, obj2) => {
return isEqualWith(obj1, obj2, (v1, v2, key) => {
return key === 'start' || key === 'end' || key === 'loc' ? true : undefined;
});
};

0 comments on commit b10733e

Please sign in to comment.