Comparing version 0.3.5 to 0.3.6
97
index.js
@@ -7,2 +7,26 @@ module.exports = Traverse; | ||
Traverse.prototype.get = function (ps) { | ||
var node = this.value; | ||
for (var i = 0; i < ps.length; i ++) { | ||
var key = ps[i]; | ||
if (!Object.hasOwnProperty.call(node, key)) { | ||
node = undefined; | ||
break; | ||
} | ||
node = node[key]; | ||
} | ||
return node; | ||
}; | ||
Traverse.prototype.set = function (ps, value) { | ||
var node = this.value; | ||
for (var i = 0; i < ps.length - 1; i ++) { | ||
var key = ps[i]; | ||
if (!Object.hasOwnProperty.call(node, key)) node[key] = {}; | ||
node = node[key]; | ||
} | ||
node[ps[i]] = value; | ||
return value; | ||
}; | ||
Traverse.prototype.map = function (cb) { | ||
@@ -28,2 +52,75 @@ return walk(this.value, cb, true); | ||
Traverse.prototype.deepEqual = function (obj) { | ||
var equal = true; | ||
var node = obj; | ||
this.forEach(function (y) { | ||
var notEqual = (function () { | ||
equal = false; | ||
//this.stop(); | ||
}).bind(this); | ||
if (this.key) node = node[this.key]; | ||
var x = node; | ||
this.post(function () { | ||
node = x; | ||
}); | ||
var toS = function (o) { | ||
return Object.prototype.toString.call(o); | ||
}; | ||
if (this.circular) { | ||
if (Traverse(obj).get(this.circular.path) !== x) notEqual(); | ||
} | ||
else if (typeof x !== typeof y) { | ||
notEqual(); | ||
} | ||
else if (x.__proto__ !== y.__proto__) { | ||
notEqual(); | ||
} | ||
else if (x === y) { | ||
// nop | ||
} | ||
else if (typeof x === 'function') { | ||
if (x instanceof RegExp) { | ||
// both regexps on account of the __proto__ check | ||
if (x.toString() != y.toString()) notEqual(); | ||
} | ||
else if (x !== y) notEqual(); | ||
} | ||
else if (typeof x === 'object') { | ||
if (x === null || y === null) { | ||
if (x !== y) notEqual(); | ||
} | ||
else if (toS(y) === '[object Arguments]' | ||
|| toS(x) === '[object Arguments]') { | ||
if (toS(x) !== toS(y)) { | ||
notEqual(); | ||
} | ||
} | ||
else if (x instanceof Date || y instanceof Date) { | ||
if (!(x instanceof Date) || !(y instanceof Date) | ||
|| x.getTime() !== y.getTime()) { | ||
notEqual(); | ||
} | ||
} | ||
else { | ||
var kx = Object.keys(x); | ||
var ky = Object.keys(y); | ||
if (kx.length !== ky.length) return false; | ||
for (var i = 0; i < kx.length; i++) { | ||
var k = kx[i]; | ||
if (!Object.hasOwnProperty.call(y, k)) { | ||
notEqual(); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
return equal; | ||
}; | ||
Traverse.prototype.paths = function () { | ||
@@ -30,0 +127,0 @@ var acc = []; |
{ | ||
"name" : "traverse", | ||
"version" : "0.3.5", | ||
"version" : "0.3.6", | ||
"description" : "Traverse and transform objects by visiting every node on a recursive walk", | ||
@@ -5,0 +5,0 @@ "author" : "James Halliday", |
Sorry, the diff of this file is not supported yet
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
30899
19
765
248