Comparing version 0.6.1 to 0.6.2
18
index.js
@@ -144,3 +144,3 @@ var traverse = module.exports = function (obj) { | ||
remove : function (stopHere) { | ||
if (Array_isArray(state.parent.node)) { | ||
if (isArray(state.parent.node)) { | ||
state.parent.node.splice(state.key, 1); | ||
@@ -225,3 +225,3 @@ } | ||
if (Array_isArray(src)) { | ||
if (isArray(src)) { | ||
dst = []; | ||
@@ -250,8 +250,14 @@ } | ||
} | ||
else if (src.__proto__ || src.constructor.prototype) { | ||
var proto = src.__proto__ || src.constructor.prototype || {}; | ||
else if (src.constructor === Object) { | ||
dst = {}; | ||
} | ||
else { | ||
var proto = | ||
(src.constructor && src.constructor.prototype) | ||
|| src.__proto__ | ||
|| {} | ||
; | ||
var T = function () {}; | ||
T.prototype = proto; | ||
dst = new T; | ||
if (!dst.__proto__) dst.__proto__ = proto; | ||
} | ||
@@ -281,3 +287,3 @@ | ||
var Array_isArray = Array.isArray || function isArray (xs) { | ||
var isArray = Array.isArray || function isArray (xs) { | ||
return Object.prototype.toString.call(xs) === '[object Array]'; | ||
@@ -284,0 +290,0 @@ }; |
{ | ||
"name" : "traverse", | ||
"version" : "0.6.1", | ||
"description" : "Traverse and transform objects by visiting every node on a recursive walk", | ||
"author" : "James Halliday", | ||
"license" : "MIT/X11", | ||
"main" : "./index", | ||
"version" : "0.6.2", | ||
"description" : "traverse and transform objects by visiting every node on a recursive walk", | ||
"main" : "index.js", | ||
"bin" : {}, | ||
"directories" : { | ||
"example" : "example", | ||
"test" : "test" | ||
}, | ||
"dependencies" : {}, | ||
"devDependencies" : { | ||
"tap" : "~0.2.5" | ||
}, | ||
"scripts" : { | ||
"test" : "tap test/*.js" | ||
}, | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "http://github.com/substack/js-traverse.git" | ||
"url" : "git://github.com/substack/js-traverse.git" | ||
}, | ||
"devDependencies" : { | ||
"expresso" : "0.7.x" | ||
"homepage" : "https://github.com/substack/js-traverse", | ||
"keywords" : [ | ||
"traverse", | ||
"walk", | ||
"recursive", | ||
"map", | ||
"forEach", | ||
"deep", | ||
"clone" | ||
], | ||
"author" : { | ||
"name" : "James Halliday", | ||
"email" : "mail@substack.net", | ||
"url" : "http://substack.net" | ||
}, | ||
"scripts" : { | ||
"test" : "expresso" | ||
} | ||
"license" : "MIT", | ||
"engine" : { "node" : ">=0.6" } | ||
} |
@@ -1,23 +0,23 @@ | ||
var assert = require('assert'); | ||
var Traverse = require('../'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
var deepEqual = require('./lib/deep_equal'); | ||
var util = require('util'); | ||
exports.circular = function () { | ||
test('circular', function (t) { | ||
t.plan(1); | ||
var obj = { x : 3 }; | ||
obj.y = obj; | ||
var foundY = false; | ||
Traverse(obj).forEach(function (x) { | ||
traverse(obj).forEach(function (x) { | ||
if (this.path.join('') == 'y') { | ||
assert.equal( | ||
t.equal( | ||
util.inspect(this.circular.node), | ||
util.inspect(obj) | ||
); | ||
foundY = true; | ||
} | ||
}); | ||
assert.ok(foundY); | ||
}; | ||
}); | ||
exports.deepCirc = function () { | ||
test('deepCirc', function (t) { | ||
t.plan(2); | ||
var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; | ||
@@ -27,14 +27,11 @@ obj.y[2] = obj; | ||
var times = 0; | ||
Traverse(obj).forEach(function (x) { | ||
traverse(obj).forEach(function (x) { | ||
if (this.circular) { | ||
assert.deepEqual(this.circular.path, []); | ||
assert.deepEqual(this.path, [ 'y', 2 ]); | ||
times ++; | ||
t.same(this.circular.path, []); | ||
t.same(this.path, [ 'y', 2 ]); | ||
} | ||
}); | ||
assert.deepEqual(times, 1); | ||
}; | ||
}); | ||
exports.doubleCirc = function () { | ||
test('doubleCirc', function (t) { | ||
var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; | ||
@@ -45,3 +42,3 @@ obj.y[2] = obj; | ||
var circs = []; | ||
Traverse(obj).forEach(function (x) { | ||
traverse(obj).forEach(function (x) { | ||
if (this.circular) { | ||
@@ -52,12 +49,13 @@ circs.push({ circ : this.circular, self : this, node : x }); | ||
assert.deepEqual(circs[0].self.path, [ 'x', 3, 2 ]); | ||
assert.deepEqual(circs[0].circ.path, []); | ||
t.same(circs[0].self.path, [ 'x', 3, 2 ]); | ||
t.same(circs[0].circ.path, []); | ||
assert.deepEqual(circs[1].self.path, [ 'y', 2 ]); | ||
assert.deepEqual(circs[1].circ.path, []); | ||
t.same(circs[1].self.path, [ 'y', 2 ]); | ||
t.same(circs[1].circ.path, []); | ||
assert.deepEqual(circs.length, 2); | ||
}; | ||
t.same(circs.length, 2); | ||
t.end(); | ||
}); | ||
exports.circDubForEach = function () { | ||
test('circDubForEach', function (t) { | ||
var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; | ||
@@ -67,10 +65,11 @@ obj.y[2] = obj; | ||
Traverse(obj).forEach(function (x) { | ||
traverse(obj).forEach(function (x) { | ||
if (this.circular) this.update('...'); | ||
}); | ||
assert.deepEqual(obj, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] }); | ||
}; | ||
t.same(obj, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] }); | ||
t.end(); | ||
}); | ||
exports.circDubMap = function () { | ||
test('circDubMap', function (t) { | ||
var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; | ||
@@ -80,3 +79,3 @@ obj.y[2] = obj; | ||
var c = Traverse(obj).map(function (x) { | ||
var c = traverse(obj).map(function (x) { | ||
if (this.circular) { | ||
@@ -87,6 +86,7 @@ this.update('...'); | ||
assert.deepEqual(c, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] }); | ||
}; | ||
t.same(c, { x : [ 1, 2, 3, [ 4, 5, '...' ] ], y : [ 4, 5, '...' ] }); | ||
t.end(); | ||
}); | ||
exports.circClone = function () { | ||
test('circClone', function (t) { | ||
var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; | ||
@@ -96,27 +96,29 @@ obj.y[2] = obj; | ||
var clone = Traverse.clone(obj); | ||
assert.ok(obj !== clone); | ||
var clone = traverse.clone(obj); | ||
t.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.deepEqual(clone.x.slice(0,3), [1,2,3]); | ||
assert.deepEqual(clone.y.slice(0,2), [4,5]); | ||
}; | ||
t.ok(clone.y[2] === clone); | ||
t.ok(clone.y[2] !== obj); | ||
t.ok(clone.x[3][2] === clone); | ||
t.ok(clone.x[3][2] !== obj); | ||
t.same(clone.x.slice(0,3), [1,2,3]); | ||
t.same(clone.y.slice(0,2), [4,5]); | ||
t.end(); | ||
}); | ||
exports.circMapScrub = function () { | ||
test('circMapScrub', function (t) { | ||
var obj = { a : 1, b : 2 }; | ||
obj.c = obj; | ||
var scrubbed = Traverse(obj).map(function (node) { | ||
var scrubbed = traverse(obj).map(function (node) { | ||
if (this.circular) this.remove(); | ||
}); | ||
assert.deepEqual( | ||
t.same( | ||
Object.keys(scrubbed).sort(), | ||
[ 'a', 'b' ] | ||
); | ||
assert.ok(deepEqual(scrubbed, { a : 1, b : 2 })); | ||
t.ok(deepEqual(scrubbed, { a : 1, b : 2 })); | ||
assert.equal(obj.c, obj); | ||
}; | ||
t.equal(obj.c, obj); | ||
t.end(); | ||
}); |
@@ -1,5 +0,5 @@ | ||
var assert = require('assert'); | ||
var Traverse = require('../'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
exports.dateEach = function () { | ||
test('dateEach', function (t) { | ||
var obj = { x : new Date, y : 10, z : 5 }; | ||
@@ -9,3 +9,3 @@ | ||
Traverse(obj).forEach(function (node) { | ||
traverse(obj).forEach(function (node) { | ||
var t = (node instanceof Date && 'Date') || typeof node; | ||
@@ -15,3 +15,3 @@ counts[t] = (counts[t] || 0) + 1; | ||
assert.deepEqual(counts, { | ||
t.same(counts, { | ||
object : 1, | ||
@@ -21,13 +21,14 @@ Date : 1, | ||
}); | ||
}; | ||
t.end(); | ||
}); | ||
exports.dateMap = function () { | ||
test('dateMap', function (t) { | ||
var obj = { x : new Date, y : 10, z : 5 }; | ||
var res = Traverse(obj).map(function (node) { | ||
var res = traverse(obj).map(function (node) { | ||
if (typeof node === 'number') this.update(node + 100); | ||
}); | ||
assert.ok(obj.x !== res.x); | ||
assert.deepEqual(res, { | ||
t.ok(obj.x !== res.x); | ||
t.same(res, { | ||
x : obj.x, | ||
@@ -37,3 +38,4 @@ y : 110, | ||
}); | ||
}; | ||
t.end(); | ||
}); | ||
@@ -1,7 +0,9 @@ | ||
var assert = require('assert'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
var deepEqual = require('./lib/deep_equal'); | ||
exports.deepDates = function () { | ||
assert.ok( | ||
test('deepDates', function (t) { | ||
t.plan(2); | ||
t.ok( | ||
deepEqual( | ||
@@ -16,3 +18,3 @@ { d : new Date, x : [ 1, 2, 3 ] }, | ||
setTimeout(function () { | ||
assert.ok( | ||
t.ok( | ||
!deepEqual( | ||
@@ -25,5 +27,5 @@ { d : d0, x : [ 1, 2, 3 ], }, | ||
}, 5); | ||
}; | ||
}); | ||
exports.deepCircular = function () { | ||
test('deepCircular', function (t) { | ||
var a = [1]; | ||
@@ -35,3 +37,3 @@ a.push(a); // a = [ 1, *a ] | ||
assert.ok( | ||
t.ok( | ||
!deepEqual(a, b), | ||
@@ -43,3 +45,3 @@ 'circular ref mount points count towards equality' | ||
c.push(c); // c = [ 1, *c ] | ||
assert.ok( | ||
t.ok( | ||
deepEqual(a, c), | ||
@@ -51,10 +53,12 @@ 'circular refs are structurally the same here' | ||
d.push(a); // c = [ 1, [ 1, *d ] ] | ||
assert.ok( | ||
t.ok( | ||
deepEqual(b, d), | ||
'non-root circular ref structural comparison' | ||
); | ||
}; | ||
t.end(); | ||
}); | ||
exports.deepInstances = function () { | ||
assert.ok( | ||
test('deepInstances', function (t) { | ||
t.ok( | ||
!deepEqual([ new Boolean(false) ], [ false ]), | ||
@@ -64,3 +68,3 @@ 'boolean instances are not real booleans' | ||
assert.ok( | ||
t.ok( | ||
!deepEqual([ new String('x') ], [ 'x' ]), | ||
@@ -70,3 +74,3 @@ 'string instances are not real strings' | ||
assert.ok( | ||
t.ok( | ||
!deepEqual([ new Number(4) ], [ 4 ]), | ||
@@ -76,3 +80,3 @@ 'number instances are not real numbers' | ||
assert.ok( | ||
t.ok( | ||
deepEqual([ new RegExp('x') ], [ /x/ ]), | ||
@@ -82,3 +86,3 @@ 'regexp instances are real regexps' | ||
assert.ok( | ||
t.ok( | ||
!deepEqual([ new RegExp(/./) ], [ /../ ]), | ||
@@ -88,3 +92,3 @@ 'these regexps aren\'t the same' | ||
assert.ok( | ||
t.ok( | ||
!deepEqual( | ||
@@ -98,17 +102,20 @@ [ function (x) { return x * 2 } ], | ||
var f = function (x) { return x * 2 }; | ||
assert.ok( | ||
t.ok( | ||
deepEqual([ f ], [ f ]), | ||
'these functions are actually equal' | ||
); | ||
}; | ||
t.end(); | ||
}); | ||
exports.deepEqual = function () { | ||
assert.ok( | ||
test('deepEqual', function (t) { | ||
t.ok( | ||
!deepEqual([ 1, 2, 3 ], { 0 : 1, 1 : 2, 2 : 3 }), | ||
'arrays are not objects' | ||
); | ||
}; | ||
t.end(); | ||
}); | ||
exports.falsy = function () { | ||
assert.ok( | ||
test('falsy', function (t) { | ||
t.ok( | ||
!deepEqual([ undefined ], [ null ]), | ||
@@ -118,3 +125,3 @@ 'null is not undefined!' | ||
assert.ok( | ||
t.ok( | ||
!deepEqual([ null ], [ undefined ]), | ||
@@ -124,3 +131,3 @@ 'undefined is not null!' | ||
assert.ok( | ||
t.ok( | ||
!deepEqual( | ||
@@ -133,3 +140,3 @@ { a : 1, b : 2, c : [ 3, undefined, 5 ] }, | ||
assert.ok( | ||
t.ok( | ||
!deepEqual( | ||
@@ -142,3 +149,3 @@ { a : 1, b : 2, c : [ 3, undefined, 5 ] }, | ||
assert.ok( | ||
t.ok( | ||
!deepEqual( | ||
@@ -150,5 +157,7 @@ { a : 1, b : 2, c : [ 3, undefined, 5 ] }, | ||
); | ||
}; | ||
t.end(); | ||
}); | ||
exports.deletedArrayEqual = function () { | ||
test('deletedArrayEqual', function (t) { | ||
var xs = [ 1, 2, 3, 4 ]; | ||
@@ -162,3 +171,3 @@ delete xs[2]; | ||
assert.ok( | ||
t.ok( | ||
deepEqual(xs, ys), | ||
@@ -169,3 +178,3 @@ 'arrays with deleted elements are only equal to' | ||
assert.ok( | ||
t.ok( | ||
!deepEqual(xs, [ 1, 2, undefined, 4 ]), | ||
@@ -175,13 +184,15 @@ 'deleted array elements cannot be undefined' | ||
assert.ok( | ||
t.ok( | ||
!deepEqual(xs, [ 1, 2, null, 4 ]), | ||
'deleted array elements cannot be null' | ||
); | ||
}; | ||
t.end(); | ||
}); | ||
exports.deletedObjectEqual = function () { | ||
test('deletedObjectEqual', function (t) { | ||
var obj = { a : 1, b : 2, c : 3 }; | ||
delete obj.c; | ||
assert.ok( | ||
t.ok( | ||
deepEqual(obj, { a : 1, b : 2 }), | ||
@@ -191,3 +202,3 @@ 'deleted object elements should not show up' | ||
assert.ok( | ||
t.ok( | ||
!deepEqual(obj, { a : 1, b : 2, c : undefined }), | ||
@@ -197,16 +208,20 @@ 'deleted object elements are not undefined' | ||
assert.ok( | ||
t.ok( | ||
!deepEqual(obj, { a : 1, b : 2, c : null }), | ||
'deleted object elements are not null' | ||
); | ||
}; | ||
t.end(); | ||
}); | ||
exports.emptyKeyEqual = function () { | ||
assert.ok(!deepEqual( | ||
test('emptyKeyEqual', function (t) { | ||
t.ok(!deepEqual( | ||
{ a : 1 }, { a : 1, '' : 55 } | ||
)); | ||
}; | ||
t.end(); | ||
}); | ||
exports.deepArguments = function () { | ||
assert.ok( | ||
test('deepArguments', function (t) { | ||
t.ok( | ||
!deepEqual( | ||
@@ -219,3 +234,3 @@ [ 4, 5, 6 ], | ||
assert.ok( | ||
t.ok( | ||
deepEqual( | ||
@@ -227,17 +242,22 @@ (function () { return arguments })(4, 5, 6), | ||
); | ||
}; | ||
t.end(); | ||
}); | ||
exports.deepUn = function () { | ||
assert.ok(!deepEqual({ a : 1, b : 2 }, undefined)); | ||
assert.ok(!deepEqual({ a : 1, b : 2 }, {})); | ||
assert.ok(!deepEqual(undefined, { a : 1, b : 2 })); | ||
assert.ok(!deepEqual({}, { a : 1, b : 2 })); | ||
assert.ok(deepEqual(undefined, undefined)); | ||
assert.ok(deepEqual(null, null)); | ||
assert.ok(!deepEqual(undefined, null)); | ||
}; | ||
test('deepUn', function (t) { | ||
t.ok(!deepEqual({ a : 1, b : 2 }, undefined)); | ||
t.ok(!deepEqual({ a : 1, b : 2 }, {})); | ||
t.ok(!deepEqual(undefined, { a : 1, b : 2 })); | ||
t.ok(!deepEqual({}, { a : 1, b : 2 })); | ||
t.ok(deepEqual(undefined, undefined)); | ||
t.ok(deepEqual(null, null)); | ||
t.ok(!deepEqual(undefined, null)); | ||
t.end(); | ||
}); | ||
exports.deepLevels = function () { | ||
test('deepLevels', function (t) { | ||
var xs = [ 1, 2, [ 3, 4, [ 5, 6 ] ] ]; | ||
assert.ok(!deepEqual(xs, [])); | ||
}; | ||
t.ok(!deepEqual(xs, [])); | ||
t.end(); | ||
}); |
@@ -1,13 +0,11 @@ | ||
var assert = require('assert'); | ||
var Traverse = require('../'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
exports['traverse an Error'] = function () { | ||
test('traverse an Error', function (t) { | ||
var obj = new Error("test"); | ||
var results = traverse(obj).map(function (node) {}); | ||
t.same(results, { message: 'test' }); | ||
t.end(); | ||
}); | ||
var results = Traverse(obj).map(function (node) { }); | ||
assert.deepEqual(results, { | ||
message: 'test' | ||
}); | ||
}; | ||
@@ -1,13 +0,15 @@ | ||
var assert = require('assert'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
exports.has = function () { | ||
test('has', function (t) { | ||
var obj = { a : 2, b : [ 4, 5, { c : 6 } ] }; | ||
assert.equal(traverse(obj).has([ 'b', 2, 'c' ]), true) | ||
assert.equal(traverse(obj).has([ 'b', 2, 'c', 0 ]), false) | ||
assert.equal(traverse(obj).has([ 'b', 2, 'd' ]), false) | ||
assert.equal(traverse(obj).has([]), true) | ||
assert.equal(traverse(obj).has([ 'a' ]), true) | ||
assert.equal(traverse(obj).has([ 'a', 2 ]), false) | ||
}; | ||
t.equal(traverse(obj).has([ 'b', 2, 'c' ]), true) | ||
t.equal(traverse(obj).has([ 'b', 2, 'c', 0 ]), false) | ||
t.equal(traverse(obj).has([ 'b', 2, 'd' ]), false) | ||
t.equal(traverse(obj).has([]), true) | ||
t.equal(traverse(obj).has([ 'a' ]), true) | ||
t.equal(traverse(obj).has([ 'a', 2 ]), false) | ||
t.end(); | ||
}); |
@@ -1,10 +0,9 @@ | ||
var assert = require('assert'); | ||
var Traverse = require('../'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
var EventEmitter = require('events').EventEmitter; | ||
exports['check instanceof on node elems'] = function () { | ||
test('check instanceof on node elems', function (t) { | ||
var counts = { emitter : 0 }; | ||
Traverse([ new EventEmitter, 3, 4, { ev : new EventEmitter }]) | ||
traverse([ new EventEmitter, 3, 4, { ev : new EventEmitter }]) | ||
.forEach(function (node) { | ||
@@ -15,4 +14,5 @@ if (node instanceof EventEmitter) counts.emitter ++; | ||
assert.equal(counts.emitter, 2); | ||
}; | ||
t.equal(counts.emitter, 2); | ||
t.end(); | ||
}); |
@@ -1,9 +0,9 @@ | ||
var assert = require('assert'); | ||
var Traverse = require('../'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
exports['interface map'] = function () { | ||
test('interface map', function (t) { | ||
var obj = { a : [ 5,6,7 ], b : { c : [8] } }; | ||
assert.deepEqual( | ||
Traverse.paths(obj) | ||
t.same( | ||
traverse.paths(obj) | ||
.sort() | ||
@@ -17,4 +17,4 @@ .map(function (path) { return path.join('/') }) | ||
assert.deepEqual( | ||
Traverse.nodes(obj), | ||
t.same( | ||
traverse.nodes(obj), | ||
[ | ||
@@ -27,4 +27,4 @@ { a: [ 5, 6, 7 ], b: { c: [ 8 ] } }, | ||
assert.deepEqual( | ||
Traverse.map(obj, function (node) { | ||
t.same( | ||
traverse.map(obj, function (node) { | ||
if (typeof node == 'number') { | ||
@@ -41,5 +41,6 @@ return node + 1000; | ||
var nodes = 0; | ||
Traverse.forEach(obj, function (node) { nodes ++ }); | ||
assert.deepEqual(nodes, 8); | ||
}; | ||
traverse.forEach(obj, function (node) { nodes ++ }); | ||
t.same(nodes, 8); | ||
t.end(); | ||
}); |
@@ -1,5 +0,5 @@ | ||
var assert = require('assert'); | ||
var Traverse = require('../'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
exports['json test'] = function () { | ||
test('json test', function (t) { | ||
var id = 54; | ||
@@ -9,3 +9,3 @@ var callbacks = {}; | ||
var scrubbed = Traverse(obj).map(function (x) { | ||
var scrubbed = traverse(obj).map(function (x) { | ||
if (typeof x === 'function') { | ||
@@ -18,3 +18,3 @@ callbacks[id] = { id : id, f : x, path : this.path }; | ||
assert.equal( | ||
t.equal( | ||
scrubbed.moo, '[Function]', | ||
@@ -24,3 +24,3 @@ 'obj.moo replaced with "[Function]"' | ||
assert.equal( | ||
t.equal( | ||
scrubbed.foo[3], '[Function]', | ||
@@ -30,3 +30,3 @@ 'obj.foo[3] replaced with "[Function]"' | ||
assert.deepEqual(scrubbed, { | ||
t.same(scrubbed, { | ||
moo : '[Function]', | ||
@@ -36,3 +36,3 @@ foo : [ 2, 3, 4, "[Function]" ] | ||
assert.deepEqual( | ||
t.same( | ||
typeof obj.moo, 'function', | ||
@@ -42,3 +42,3 @@ 'Original obj.moo still a function' | ||
assert.deepEqual( | ||
t.same( | ||
typeof obj.foo[3], 'function', | ||
@@ -48,7 +48,9 @@ 'Original obj.foo[3] still a function' | ||
assert.deepEqual(callbacks, { | ||
t.same(callbacks, { | ||
54: { id: 54, f : obj.moo, path: [ 'moo' ] }, | ||
55: { id: 55, f : obj.foo[3], path: [ 'foo', '3' ] }, | ||
}, 'Check the generated callbacks list'); | ||
}; | ||
t.end(); | ||
}); | ||
@@ -1,7 +0,7 @@ | ||
var assert = require('assert'); | ||
var Traverse = require('../'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
exports['sort test'] = function () { | ||
test('sort test', function (t) { | ||
var acc = []; | ||
Traverse({ | ||
traverse({ | ||
a: 30, | ||
@@ -24,3 +24,3 @@ b: 22, | ||
assert.equal( | ||
t.equal( | ||
acc.join(' '), | ||
@@ -30,2 +30,4 @@ '9 30 22', | ||
); | ||
}; | ||
t.end(); | ||
}); |
@@ -1,7 +0,7 @@ | ||
var assert = require('assert'); | ||
var Traverse = require('../'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
exports['leaves test'] = function () { | ||
test('leaves test', function (t) { | ||
var acc = []; | ||
Traverse({ | ||
traverse({ | ||
a : [1,2,3], | ||
@@ -15,3 +15,3 @@ b : 4, | ||
assert.equal( | ||
t.equal( | ||
acc.join(' '), | ||
@@ -21,3 +21,4 @@ '1 2 3 4 5 6 7 8 9', | ||
); | ||
}; | ||
t.end(); | ||
}); |
@@ -1,8 +0,8 @@ | ||
var assert = require('assert'); | ||
var Traverse = require('../'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
var deepEqual = require('./lib/deep_equal'); | ||
exports.mutate = function () { | ||
test('mutate', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
var res = Traverse(obj).forEach(function (x) { | ||
var res = traverse(obj).forEach(function (x) { | ||
if (typeof x === 'number' && x % 2 === 0) { | ||
@@ -12,9 +12,10 @@ this.update(x * 10); | ||
}); | ||
assert.deepEqual(obj, res); | ||
assert.deepEqual(obj, { a : 1, b : 20, c : [ 3, 40 ] }); | ||
}; | ||
t.same(obj, res); | ||
t.same(obj, { a : 1, b : 20, c : [ 3, 40 ] }); | ||
t.end(); | ||
}); | ||
exports.mutateT = function () { | ||
test('mutateT', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
var res = Traverse.forEach(obj, function (x) { | ||
var res = traverse.forEach(obj, function (x) { | ||
if (typeof x === 'number' && x % 2 === 0) { | ||
@@ -24,9 +25,10 @@ this.update(x * 10); | ||
}); | ||
assert.deepEqual(obj, res); | ||
assert.deepEqual(obj, { a : 1, b : 20, c : [ 3, 40 ] }); | ||
}; | ||
t.same(obj, res); | ||
t.same(obj, { a : 1, b : 20, c : [ 3, 40 ] }); | ||
t.end(); | ||
}); | ||
exports.map = function () { | ||
test('map', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
var res = Traverse(obj).map(function (x) { | ||
var res = traverse(obj).map(function (x) { | ||
if (typeof x === 'number' && x % 2 === 0) { | ||
@@ -36,9 +38,10 @@ this.update(x * 10); | ||
}); | ||
assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] }); | ||
assert.deepEqual(res, { a : 1, b : 20, c : [ 3, 40 ] }); | ||
}; | ||
t.same(obj, { a : 1, b : 2, c : [ 3, 4 ] }); | ||
t.same(res, { a : 1, b : 20, c : [ 3, 40 ] }); | ||
t.end(); | ||
}); | ||
exports.mapT = function () { | ||
test('mapT', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
var res = Traverse.map(obj, function (x) { | ||
var res = traverse.map(obj, function (x) { | ||
if (typeof x === 'number' && x % 2 === 0) { | ||
@@ -48,56 +51,62 @@ this.update(x * 10); | ||
}); | ||
assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] }); | ||
assert.deepEqual(res, { a : 1, b : 20, c : [ 3, 40 ] }); | ||
}; | ||
t.same(obj, { a : 1, b : 2, c : [ 3, 4 ] }); | ||
t.same(res, { a : 1, b : 20, c : [ 3, 40 ] }); | ||
t.end(); | ||
}); | ||
exports.clone = function () { | ||
test('clone', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
var res = Traverse(obj).clone(); | ||
assert.deepEqual(obj, res); | ||
assert.ok(obj !== res); | ||
var res = traverse(obj).clone(); | ||
t.same(obj, res); | ||
t.ok(obj !== res); | ||
obj.a ++; | ||
assert.deepEqual(res.a, 1); | ||
t.same(res.a, 1); | ||
obj.c.push(5); | ||
assert.deepEqual(res.c, [ 3, 4 ]); | ||
}; | ||
t.same(res.c, [ 3, 4 ]); | ||
t.end(); | ||
}); | ||
exports.cloneT = function () { | ||
test('cloneT', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
var res = Traverse.clone(obj); | ||
assert.deepEqual(obj, res); | ||
assert.ok(obj !== res); | ||
var res = traverse.clone(obj); | ||
t.same(obj, res); | ||
t.ok(obj !== res); | ||
obj.a ++; | ||
assert.deepEqual(res.a, 1); | ||
t.same(res.a, 1); | ||
obj.c.push(5); | ||
assert.deepEqual(res.c, [ 3, 4 ]); | ||
}; | ||
t.same(res.c, [ 3, 4 ]); | ||
t.end(); | ||
}); | ||
exports.reduce = function () { | ||
test('reduce', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
var res = Traverse(obj).reduce(function (acc, x) { | ||
var res = traverse(obj).reduce(function (acc, x) { | ||
if (this.isLeaf) acc.push(x); | ||
return acc; | ||
}, []); | ||
assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] }); | ||
assert.deepEqual(res, [ 1, 2, 3, 4 ]); | ||
}; | ||
t.same(obj, { a : 1, b : 2, c : [ 3, 4 ] }); | ||
t.same(res, [ 1, 2, 3, 4 ]); | ||
t.end(); | ||
}); | ||
exports.reduceInit = function () { | ||
test('reduceInit', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
var res = Traverse(obj).reduce(function (acc, x) { | ||
var res = traverse(obj).reduce(function (acc, x) { | ||
if (this.isRoot) assert.fail('got root'); | ||
return acc; | ||
}); | ||
assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] }); | ||
assert.deepEqual(res, obj); | ||
}; | ||
t.same(obj, { a : 1, b : 2, c : [ 3, 4 ] }); | ||
t.same(res, obj); | ||
t.end(); | ||
}); | ||
exports.remove = function () { | ||
test('remove', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
Traverse(obj).forEach(function (x) { | ||
traverse(obj).forEach(function (x) { | ||
if (this.isLeaf && x % 2 == 0) this.remove(); | ||
}); | ||
assert.deepEqual(obj, { a : 1, c : [ 3 ] }); | ||
}; | ||
t.same(obj, { a : 1, c : [ 3 ] }); | ||
t.end(); | ||
}); | ||
@@ -108,3 +117,3 @@ exports.removeNoStop = function() { | ||
var keys = []; | ||
Traverse(obj).forEach(function (x) { | ||
traverse(obj).forEach(function (x) { | ||
keys.push(this.key) | ||
@@ -114,3 +123,4 @@ if (this.key == 'c') this.remove(); | ||
assert.deepEqual(keys, [undefined, 'a', 'b', 'c', 'd', 'e', 'f']) | ||
t.same(keys, [undefined, 'a', 'b', 'c', 'd', 'e', 'f']) | ||
t.end(); | ||
} | ||
@@ -122,3 +132,3 @@ | ||
var keys = []; | ||
Traverse(obj).forEach(function (x) { | ||
traverse(obj).forEach(function (x) { | ||
keys.push(this.key) | ||
@@ -128,39 +138,42 @@ if (this.key == 'c') this.remove(true); | ||
assert.deepEqual(keys, [undefined, 'a', 'b', 'c', 'f']) | ||
t.same(keys, [undefined, 'a', 'b', 'c', 'f']) | ||
t.end(); | ||
} | ||
exports.removeMap = function () { | ||
test('removeMap', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
var res = Traverse(obj).map(function (x) { | ||
var res = traverse(obj).map(function (x) { | ||
if (this.isLeaf && x % 2 == 0) this.remove(); | ||
}); | ||
assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] }); | ||
assert.deepEqual(res, { a : 1, c : [ 3 ] }); | ||
}; | ||
t.same(obj, { a : 1, b : 2, c : [ 3, 4 ] }); | ||
t.same(res, { a : 1, c : [ 3 ] }); | ||
t.end(); | ||
}); | ||
exports.delete = function () { | ||
test('delete', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
Traverse(obj).forEach(function (x) { | ||
traverse(obj).forEach(function (x) { | ||
if (this.isLeaf && x % 2 == 0) this.delete(); | ||
}); | ||
assert.ok(!deepEqual( | ||
t.ok(!deepEqual( | ||
obj, { a : 1, c : [ 3, undefined ] } | ||
)); | ||
assert.ok(deepEqual( | ||
t.ok(deepEqual( | ||
obj, { a : 1, c : [ 3 ] } | ||
)); | ||
assert.ok(!deepEqual( | ||
t.ok(!deepEqual( | ||
obj, { a : 1, c : [ 3, null ] } | ||
)); | ||
}; | ||
t.end(); | ||
}); | ||
exports.deleteNoStop = function() { | ||
test('deleteNoStop', function (t) { | ||
var obj = { a : 1, b : 2, c : { d: 3, e: 4 } }; | ||
var keys = []; | ||
Traverse(obj).forEach(function (x) { | ||
traverse(obj).forEach(function (x) { | ||
keys.push(this.key) | ||
@@ -170,10 +183,11 @@ if (this.key == 'c') this.delete(); | ||
assert.deepEqual(keys, [undefined, 'a', 'b', 'c', 'd', 'e']) | ||
} | ||
t.same(keys, [undefined, 'a', 'b', 'c', 'd', 'e']) | ||
t.end(); | ||
}); | ||
exports.deleteStop = function() { | ||
test('deleteStop', function (t) { | ||
var obj = { a : 1, b : 2, c : { d: 3, e: 4 } }; | ||
var keys = []; | ||
Traverse(obj).forEach(function (x) { | ||
traverse(obj).forEach(function (x) { | ||
keys.push(this.key) | ||
@@ -183,35 +197,38 @@ if (this.key == 'c') this.delete(true); | ||
assert.deepEqual(keys, [undefined, 'a', 'b', 'c']) | ||
} | ||
t.same(keys, [undefined, 'a', 'b', 'c']) | ||
t.end(); | ||
}); | ||
exports.deleteRedux = function () { | ||
test('deleteRedux', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4, 5 ] }; | ||
Traverse(obj).forEach(function (x) { | ||
traverse(obj).forEach(function (x) { | ||
if (this.isLeaf && x % 2 == 0) this.delete(); | ||
}); | ||
assert.ok(!deepEqual( | ||
t.ok(!deepEqual( | ||
obj, { a : 1, c : [ 3, undefined, 5 ] } | ||
)); | ||
assert.ok(deepEqual( | ||
t.ok(deepEqual( | ||
obj, { a : 1, c : [ 3 ,, 5 ] } | ||
)); | ||
assert.ok(!deepEqual( | ||
t.ok(!deepEqual( | ||
obj, { a : 1, c : [ 3, null, 5 ] } | ||
)); | ||
assert.ok(!deepEqual( | ||
t.ok(!deepEqual( | ||
obj, { a : 1, c : [ 3, 5 ] } | ||
)); | ||
}; | ||
t.end(); | ||
}); | ||
exports.deleteMap = function () { | ||
test('deleteMap', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4 ] }; | ||
var res = Traverse(obj).map(function (x) { | ||
var res = traverse(obj).map(function (x) { | ||
if (this.isLeaf && x % 2 == 0) this.delete(); | ||
}); | ||
assert.ok(deepEqual( | ||
t.ok(deepEqual( | ||
obj, | ||
@@ -224,22 +241,24 @@ { a : 1, b : 2, c : [ 3, 4 ] } | ||
assert.ok(deepEqual( | ||
t.ok(deepEqual( | ||
res, { a : 1, c : xs } | ||
)); | ||
assert.ok(deepEqual( | ||
t.ok(deepEqual( | ||
res, { a : 1, c : [ 3, ] } | ||
)); | ||
assert.ok(deepEqual( | ||
t.ok(deepEqual( | ||
res, { a : 1, c : [ 3 ] } | ||
)); | ||
}; | ||
t.end(); | ||
}); | ||
exports.deleteMapRedux = function () { | ||
test('deleteMapRedux', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 3, 4, 5 ] }; | ||
var res = Traverse(obj).map(function (x) { | ||
var res = traverse(obj).map(function (x) { | ||
if (this.isLeaf && x % 2 == 0) this.delete(); | ||
}); | ||
assert.ok(deepEqual( | ||
t.ok(deepEqual( | ||
obj, | ||
@@ -252,13 +271,15 @@ { a : 1, b : 2, c : [ 3, 4, 5 ] } | ||
assert.ok(deepEqual( | ||
t.ok(deepEqual( | ||
res, { a : 1, c : xs } | ||
)); | ||
assert.ok(!deepEqual( | ||
t.ok(!deepEqual( | ||
res, { a : 1, c : [ 3, 5 ] } | ||
)); | ||
assert.ok(deepEqual( | ||
t.ok(deepEqual( | ||
res, { a : 1, c : [ 3 ,, 5 ] } | ||
)); | ||
}; | ||
t.end(); | ||
}); |
@@ -1,11 +0,11 @@ | ||
var Traverse = require('../'); | ||
var assert = require('assert'); | ||
var traverse = require('../'); | ||
var test = require('tap').test; | ||
exports['negative update test'] = function () { | ||
test('negative update test', function (t) { | ||
var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ]; | ||
var fixed = Traverse.map(obj, function (x) { | ||
var fixed = traverse.map(obj, function (x) { | ||
if (x < 0) this.update(x + 128); | ||
}); | ||
assert.deepEqual(fixed, | ||
t.same(fixed, | ||
[ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ], | ||
@@ -15,7 +15,8 @@ 'Negative values += 128' | ||
assert.deepEqual(obj, | ||
t.same(obj, | ||
[ 5, 6, -3, [ 7, 8, -2, 1 ], { f: 10, g: -13 } ], | ||
'Original references not modified' | ||
); | ||
} | ||
t.end(); | ||
}); |
@@ -1,15 +0,11 @@ | ||
var assert = require('assert'); | ||
var Traverse = require('../'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
exports['traverse an object with nested functions'] = function () { | ||
var to = setTimeout(function () { | ||
assert.fail('never ran'); | ||
}, 1000); | ||
test('traverse an object with nested functions', function (t) { | ||
t.plan(1); | ||
function Cons (x) { | ||
clearTimeout(to); | ||
assert.equal(x, 10); | ||
t.equal(x, 10) | ||
}; | ||
Traverse(new Cons(10)); | ||
}; | ||
traverse(new Cons(10)); | ||
}); |
@@ -1,5 +0,5 @@ | ||
var assert = require('assert'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
exports.siblings = function () { | ||
test('siblings', function (t) { | ||
var obj = { a : 1, b : 2, c : [ 4, 5, 6 ] }; | ||
@@ -26,3 +26,3 @@ | ||
assert.deepEqual(res, { | ||
t.same(res, { | ||
'/' : { siblings : [], key : undefined, index : -1 }, | ||
@@ -36,2 +36,4 @@ '/a' : { siblings : [ 'a', 'b', 'c' ], key : 'a', index : 0 }, | ||
}); | ||
}; | ||
t.end(); | ||
}); |
@@ -1,5 +0,5 @@ | ||
var assert = require('assert'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
exports.stop = function () { | ||
test('stop', function (t) { | ||
var visits = 0; | ||
@@ -13,6 +13,7 @@ traverse('abcdefghij'.split('')).forEach(function (node) { | ||
assert.equal(visits, 5); | ||
}; | ||
t.equal(visits, 5); | ||
t.end(); | ||
}); | ||
exports.stopMap = function () { | ||
test('stopMap', function (t) { | ||
var s = traverse('abcdefghij'.split('')).map(function (node) { | ||
@@ -25,6 +26,7 @@ if (typeof node === 'string') { | ||
assert.equal(s, 'ABCDEfghij'); | ||
}; | ||
t.equal(s, 'ABCDEfghij'); | ||
t.end(); | ||
}); | ||
exports.stopReduce = function () { | ||
test('stopReduce', function (t) { | ||
var obj = { | ||
@@ -42,3 +44,4 @@ a : [ 4, 5 ], | ||
assert.deepEqual(xs, [ 4, 5, 6 ]); | ||
}; | ||
t.same(xs, [ 4, 5, 6 ]); | ||
t.end(); | ||
}); |
@@ -1,9 +0,9 @@ | ||
var assert = require('assert'); | ||
var Traverse = require('../'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
exports.stringify = function () { | ||
test('stringify', function (t) { | ||
var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ]; | ||
var s = ''; | ||
Traverse(obj).forEach(function (node) { | ||
traverse(obj).forEach(function (node) { | ||
if (Array.isArray(node)) { | ||
@@ -34,4 +34,4 @@ this.before(function () { s += '[' }); | ||
assert.equal(s, JSON.stringify(obj)); | ||
} | ||
t.equal(s, JSON.stringify(obj)); | ||
t.end(); | ||
}); |
var traverse = require('../'); | ||
var assert = require('assert'); | ||
var test = require('tap').test; | ||
exports.subexpr = function () { | ||
test('subexpr', function (t) { | ||
var obj = [ 'a', 4, 'b', 5, 'c', 6 ]; | ||
@@ -12,4 +12,4 @@ var r = traverse(obj).map(function (x) { | ||
assert.deepEqual(obj, [ 'a', 4, 'b', 5, 'c', 6 ]); | ||
assert.deepEqual(r, [ | ||
t.same(obj, [ 'a', 4, 'b', 5, 'c', 6 ]); | ||
t.same(r, [ | ||
'a', [ 3.9, 4, 4.1 ], | ||
@@ -19,5 +19,6 @@ 'b', [ 4.9, 5, 5.1 ], | ||
]); | ||
}; | ||
t.end(); | ||
}); | ||
exports.block = function () { | ||
test('block', function (t) { | ||
var obj = [ [ 1 ], [ 2 ], [ 3 ] ]; | ||
@@ -31,3 +32,3 @@ var r = traverse(obj).map(function (x) { | ||
assert.deepEqual(r, [ | ||
t.same(r, [ | ||
[ [ [ [ [ 5 ] ] ] ] ], | ||
@@ -37,2 +38,3 @@ [ [ [ [ 5 ] ] ] ], | ||
]); | ||
}; | ||
t.end(); | ||
}); |
@@ -1,21 +0,22 @@ | ||
var assert = require('assert'); | ||
var test = require('tap').test; | ||
var traverse = require('../'); | ||
var deepEqual = require('./lib/deep_equal'); | ||
exports.super_deep = function () { | ||
test('super_deep', function (t) { | ||
var util = require('util'); | ||
var a0 = make(); | ||
var a1 = make(); | ||
assert.ok(deepEqual(a0, a1)); | ||
t.ok(deepEqual(a0, a1)); | ||
a0.c.d.moo = true; | ||
assert.ok(!deepEqual(a0, a1)); | ||
t.ok(!deepEqual(a0, a1)); | ||
a1.c.d.moo = true; | ||
assert.ok(deepEqual(a0, a1)); | ||
t.ok(deepEqual(a0, a1)); | ||
// TODO: this one | ||
//a0.c.a = a1; | ||
//assert.ok(!deepEqual(a0, a1)); | ||
}; | ||
//t.ok(!deepEqual(a0, a1)); | ||
t.end(); | ||
}); | ||
@@ -22,0 +23,0 @@ function make () { |
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
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
47051
32
0
1355
259