Comparing version 0.3.0 to 0.3.1
55
index.js
@@ -8,9 +8,7 @@ module.exports = Traverse; | ||
Traverse.prototype.map = function (cb) { | ||
var obj = Traverse.clone(this.value); | ||
walk(obj, cb); | ||
return obj; | ||
return walk(this.clone(), cb); | ||
}; | ||
Traverse.prototype.forEach = function (cb) { | ||
walk(this.value, cb); | ||
this.value = walk(this.value, cb); | ||
return this.value; | ||
@@ -47,34 +45,27 @@ }; | ||
Traverse.prototype.clone = function () { | ||
// clone refObj for a properly immutable interface: | ||
var refs = []; | ||
var nodes = []; | ||
var parents = [], nodes = []; | ||
return (function clone (ref) { | ||
if (typeof ref == 'object' && ref !== null) { | ||
var node = Array.isArray(ref) ? [] : {}; | ||
refs.push(ref); | ||
nodes.push(node); | ||
return (function clone (src) { | ||
for (var i = 0; i < parents.length; i++) { | ||
if (parents[i] === src) { | ||
return nodes[i]; | ||
} | ||
} | ||
if (typeof src === 'object' && src !== null) { | ||
var dst = Array.isArray(src) ? [] : Object.create(src.__proto__); | ||
Object.keys(ref).forEach(function (key) { | ||
var i = refs.indexOf(ref[key]); | ||
if (i >= 0) { | ||
node[key] = nodes[i]; | ||
} | ||
else { | ||
node[key] = clone(ref[key]); | ||
} | ||
parents.push(src); | ||
nodes.push(dst); | ||
Object.keys(src).forEach(function (key) { | ||
dst[key] = clone(src[key]); | ||
}); | ||
refs.pop(); | ||
parents.pop(); | ||
nodes.pop(); | ||
// To make instanceof work: | ||
if (!Array.isArray(ref)) node.__proto__ = ref.__proto__; | ||
// Probably there are other attributes worth copying | ||
return node; | ||
return dst; | ||
} | ||
else { | ||
return ref; | ||
return src; | ||
} | ||
@@ -88,3 +79,3 @@ })(this.value); | ||
return (function walker (node) { | ||
(function walker (node) { | ||
var modifiers = {}; | ||
@@ -97,3 +88,3 @@ | ||
key : path.slice(-1)[0], | ||
isRoot : node === root, | ||
isRoot : path.length === 0, | ||
level : path.length, | ||
@@ -163,2 +154,4 @@ circular : null, | ||
})(root); | ||
return root; | ||
} | ||
@@ -165,0 +158,0 @@ |
{ | ||
"name" : "traverse", | ||
"version" : "0.3.0", | ||
"version" : "0.3.1", | ||
"description" : "Traverse and transform objects by visiting every node on a recursive walk", | ||
@@ -5,0 +5,0 @@ "author" : "James Halliday", |
@@ -57,1 +57,43 @@ var assert = require('assert'); | ||
}; | ||
exports.circDubForEach = function () { | ||
var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; | ||
obj.y[2] = obj; | ||
obj.x.push(obj.y); | ||
Traverse(obj).forEach(function (x) { | ||
if (this.circular) this.update('...'); | ||
}); | ||
assert.eql(obj, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] }); | ||
}; | ||
exports.circDubMap = function () { | ||
var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; | ||
obj.y[2] = obj; | ||
obj.x.push(obj.y); | ||
var c = Traverse(obj).map(function (x) { | ||
if (this.circular) { | ||
this.update('...'); | ||
} | ||
}); | ||
assert.eql(c, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] }); | ||
}; | ||
exports.circClone = function () { | ||
var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; | ||
obj.y[2] = obj; | ||
obj.x.push(obj.y); | ||
var clone = Traverse.clone(obj); | ||
assert.ok(obj !== clone); | ||
assert.ok(clone.y[2] === clone); | ||
assert.ok(clone.y[2] !== obj); | ||
assert.ok(clone.x[3][2] === clone); | ||
assert.ok(clone.x[3][2] !== obj); | ||
assert.eql(clone.x.slice(0,3), [1,2,3]); | ||
assert.eql(clone.y.slice(0,2), [4,5]); | ||
}; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21576
519
0