pax_global_header00006660000000000000000000000064117377504430014525gustar00rootroot0000000000000052 comment=6b78600f53284b324e733f836b9fd3b47fa5b28d substack-js-traverse-6b78600/000077500000000000000000000000001173775044300160655ustar00rootroot00000000000000substack-js-traverse-6b78600/LICENSE000066400000000000000000000023341173775044300170740ustar00rootroot00000000000000Copyright 2010 James Halliday (mail@substack.net) This project is free software released under the MIT/X11 license: http://www.opensource.org/licenses/mit-license.php Copyright 2010 James Halliday (mail@substack.net) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. substack-js-traverse-6b78600/README.markdown000066400000000000000000000112021173775044300205620ustar00rootroot00000000000000traverse ======== Traverse and transform objects by visiting every node on a recursive walk. examples ======== transform negative numbers in-place ----------------------------------- negative.js ````javascript var traverse = require('traverse'); var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ]; traverse(obj).forEach(function (x) { if (x < 0) this.update(x + 128); }); console.dir(obj); ```` Output: [ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ] collect leaf nodes ------------------ leaves.js ````javascript var traverse = require('traverse'); var obj = { a : [1,2,3], b : 4, c : [5,6], d : { e : [7,8], f : 9 }, }; var leaves = traverse(obj).reduce(function (acc, x) { if (this.isLeaf) acc.push(x); return acc; }, []); console.dir(leaves); ```` Output: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] scrub circular references ------------------------- scrub.js: ````javascript var traverse = require('traverse'); var obj = { a : 1, b : 2, c : [ 3, 4 ] }; obj.c.push(obj); var scrubbed = traverse(obj).map(function (x) { if (this.circular) this.remove() }); console.dir(scrubbed); ```` output: { a: 1, b: 2, c: [ 3, 4 ] } methods ======= Each method that takes an `fn` uses the context documented below in the context section. .map(fn) -------- Execute `fn` for each node in the object and return a new object with the results of the walk. To update nodes in the result use `this.update(value)`. .forEach(fn) ------------ Execute `fn` for each node in the object but unlike `.map()`, when `this.update()` is called it updates the object in-place. .reduce(fn, acc) ---------------- For each node in the object, perform a [left-fold](http://en.wikipedia.org/wiki/Fold_(higher-order_function)) with the return value of `fn(acc, node)`. If `acc` isn't specified, `acc` is set to the root object for the first step and the root element is skipped. .paths() -------- Return an `Array` of every possible non-cyclic path in the object. Paths are `Array`s of string keys. .nodes() -------- Return an `Array` of every node in the object. .clone() -------- Create a deep clone of the object. .get(path) ---------- Get the element at the array `path`. .set(path, value) ----------------- Set the element at the array `path` to `value`. .has(path) ---------- Return whether the element at the array `path` exists. context ======= Each method that takes a callback has a context (its `this` object) with these attributes: this.node --------- The present node on the recursive walk this.path --------- An array of string keys from the root to the present node this.parent ----------- The context of the node's parent. This is `undefined` for the root node. this.key -------- The name of the key of the present node in its parent. This is `undefined` for the root node. this.isRoot, this.notRoot ------------------------- Whether the present node is the root node this.isLeaf, this.notLeaf ------------------------- Whether or not the present node is a leaf node (has no children) this.level ---------- Depth of the node within the traversal this.circular ------------- If the node equals one of its parents, the `circular` attribute is set to the context of that parent and the traversal progresses no deeper. this.update(value, stopHere=false) ---------------------------------- Set a new value for the present node. All the elements in `value` will be recursively traversed unless `stopHere` is true. this.remove(stopHere=false) ------------- Remove the current element from the output. If the node is in an Array it will be spliced off. Otherwise it will be deleted from its parent. this.delete(stopHere=false) ------------- Delete the current element from its parent in the output. Calls `delete` even on Arrays. this.before(fn) --------------- Call this function before any of the children are traversed. You can assign into `this.keys` here to traverse in a custom order. this.after(fn) -------------- Call this function after any of the children are traversed. this.pre(fn) ------------ Call this function before each of the children are traversed. this.post(fn) ------------- Call this function after each of the children are traversed. install ======= Using [npm](http://npmjs.org) do: $ npm install traverse test ==== Using [expresso](http://github.com/visionmedia/expresso) do: $ expresso 100% wahoo, your stuff is not broken! in the browser ============== Use [browserify](https://github.com/substack/node-browserify) to run traverse in the browser. traverse has been tested and works with: * Internet Explorer 5.5, 6.0, 7.0, 8.0, 9.0 * Firefox 3.5 * Chrome 6.0 * Opera 10.6 * Safari 5.0 substack-js-traverse-6b78600/examples/000077500000000000000000000000001173775044300177035ustar00rootroot00000000000000substack-js-traverse-6b78600/examples/json.js000077500000000000000000000006061173775044300212170ustar00rootroot00000000000000var traverse = require('traverse'); var id = 54; var callbacks = {}; var obj = { moo : function () {}, foo : [2,3,4, function () {}] }; var scrubbed = traverse(obj).map(function (x) { if (typeof x === 'function') { callbacks[id] = { id : id, f : x, path : this.path }; this.update('[Function]'); id++; } }); console.dir(scrubbed); console.dir(callbacks); substack-js-traverse-6b78600/examples/leaves.js000077500000000000000000000004041173775044300215210ustar00rootroot00000000000000var traverse = require('traverse'); var obj = { a : [1,2,3], b : 4, c : [5,6], d : { e : [7,8], f : 9 }, }; var leaves = traverse(obj).reduce(function (acc, x) { if (this.isLeaf) acc.push(x); return acc; }, []); console.dir(leaves); substack-js-traverse-6b78600/examples/negative.js000077500000000000000000000003041173775044300220430ustar00rootroot00000000000000var traverse = require('traverse'); var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ]; traverse(obj).forEach(function (x) { if (x < 0) this.update(x + 128); }); console.dir(obj); substack-js-traverse-6b78600/examples/scrub.js000077500000000000000000000003621173775044300213630ustar00rootroot00000000000000// scrub out circular references var traverse = require('traverse'); var obj = { a : 1, b : 2, c : [ 3, 4 ] }; obj.c.push(obj); var scrubbed = traverse(obj).map(function (x) { if (this.circular) this.remove() }); console.dir(scrubbed); substack-js-traverse-6b78600/examples/stringify.js000077500000000000000000000020271173775044300222630ustar00rootroot00000000000000#!/usr/bin/env node var traverse = require('traverse'); var obj = [ 'five', 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ]; var s = ''; traverse(obj).forEach(function to_s (node) { if (Array.isArray(node)) { this.before(function () { s += '[' }); this.post(function (child) { if (!child.isLast) s += ','; }); this.after(function () { s += ']' }); } else if (typeof node == 'object') { this.before(function () { s += '{' }); this.pre(function (x, key) { to_s(key); s += ':'; }); this.post(function (child) { if (!child.isLast) s += ','; }); this.after(function () { s += '}' }); } else if (typeof node == 'string') { s += '"' + node.toString().replace(/"/g, '\\"') + '"'; } else if (typeof node == 'function') { s += 'null'; } else { s += node.toString(); } }); console.log('JSON.stringify: ' + JSON.stringify(obj)); console.log('this stringify: ' + s); substack-js-traverse-6b78600/index.js000066400000000000000000000204121173775044300175310ustar00rootroot00000000000000var traverse = module.exports = function (obj) { return new Traverse(obj); }; function Traverse (obj) { this.value = obj; } 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.has = function (ps) { var node = this.value; for (var i = 0; i < ps.length; i ++) { var key = ps[i]; if (!Object.hasOwnProperty.call(node, key)) { return false; } node = node[key]; } return true; }; 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) { return walk(this.value, cb, true); }; Traverse.prototype.forEach = function (cb) { this.value = walk(this.value, cb, false); return this.value; }; Traverse.prototype.reduce = function (cb, init) { var skip = arguments.length === 1; var acc = skip ? this.value : init; this.forEach(function (x) { if (!this.isRoot || !skip) { acc = cb.call(this, acc, x); } }); return acc; }; Traverse.prototype.paths = function () { var acc = []; this.forEach(function (x) { acc.push(this.path); }); return acc; }; Traverse.prototype.nodes = function () { var acc = []; this.forEach(function (x) { acc.push(this.node); }); return acc; }; Traverse.prototype.clone = function () { var parents = [], nodes = []; 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 = copy(src); parents.push(src); nodes.push(dst); forEach(Object_keys(src), function (key) { dst[key] = clone(src[key]); }); parents.pop(); nodes.pop(); return dst; } else { return src; } })(this.value); }; function walk (root, cb, immutable) { var path = []; var parents = []; var alive = true; return (function walker (node_) { var node = immutable ? copy(node_) : node_; var modifiers = {}; var keepGoing = true; var state = { node : node, node_ : node_, path : [].concat(path), parent : parents[parents.length - 1], parents : parents, key : path.slice(-1)[0], isRoot : path.length === 0, level : path.length, circular : null, update : function (x, stopHere) { if (!state.isRoot) { state.parent.node[state.key] = x; } state.node = x; if (stopHere) keepGoing = false; }, 'delete' : function (stopHere) { delete state.parent.node[state.key]; if (stopHere) keepGoing = false; }, remove : function (stopHere) { if (Array_isArray(state.parent.node)) { state.parent.node.splice(state.key, 1); } else { delete state.parent.node[state.key]; } if (stopHere) keepGoing = false; }, keys : null, before : function (f) { modifiers.before = f }, after : function (f) { modifiers.after = f }, pre : function (f) { modifiers.pre = f }, post : function (f) { modifiers.post = f }, stop : function () { alive = false }, block : function () { keepGoing = false } }; if (!alive) return state; if (typeof node === 'object' && node !== null) { state.keys = Object_keys(node); state.isLeaf = state.keys.length == 0; for (var i = 0; i < parents.length; i++) { if (parents[i].node_ === node_) { state.circular = parents[i]; break; } } } else { state.isLeaf = true; } state.notLeaf = !state.isLeaf; state.notRoot = !state.isRoot; // use return values to update if defined var ret = cb.call(state, state.node); if (ret !== undefined && state.update) state.update(ret); if (modifiers.before) modifiers.before.call(state, state.node); if (!keepGoing) return state; if (typeof state.node == 'object' && state.node !== null && !state.circular) { parents.push(state); forEach(state.keys, function (key, i) { path.push(key); if (modifiers.pre) modifiers.pre.call(state, state.node[key], key); var child = walker(state.node[key]); if (immutable && Object.hasOwnProperty.call(state.node, key)) { state.node[key] = child.node; } child.isLast = i == state.keys.length - 1; child.isFirst = i == 0; if (modifiers.post) modifiers.post.call(state, child); path.pop(); }); parents.pop(); } if (modifiers.after) modifiers.after.call(state, state.node); return state; })(root).node; } function copy (src) { if (typeof src === 'object' && src !== null) { var dst; if (Array_isArray(src)) { dst = []; } else if (isDate(src)) { dst = new Date(src); } else if (isRegExp(src)) { dst = new RegExp(src); } else if (isError(src)) { dst = { message: src.message }; } else if (isBoolean(src)) { dst = new Boolean(src); } else if (isNumber(src)) { dst = new Number(src); } else if (isString(src)) { dst = new String(src); } else if (Object.create && Object.getPrototypeOf) { dst = Object.create(Object.getPrototypeOf(src)); } else if (src.__proto__ || src.constructor.prototype) { var proto = src.__proto__ || src.constructor.prototype || {}; var T = function () {}; T.prototype = proto; dst = new T; if (!dst.__proto__) dst.__proto__ = proto; } forEach(Object_keys(src), function (key) { dst[key] = src[key]; }); return dst; } else return src; } var Object_keys = Object.keys || function keys (obj) { var res = []; for (var key in obj) res.push(key) return res; }; function toS (obj) { return Object.prototype.toString.call(obj) } function isDate (obj) { return toS(obj) === '[object Date]' } function isRegExp (obj) { return toS(obj) === '[object RegExp]' } function isError (obj) { return toS(obj) === '[object Error]' } function isBoolean (obj) { return toS(obj) === '[object Boolean]' } function isNumber (obj) { return toS(obj) === '[object Number]' } function isString (obj) { return toS(obj) === '[object String]' } var Array_isArray = Array.isArray || function isArray (xs) { return Object.prototype.toString.call(xs) === '[object Array]'; }; var forEach = function (xs, fn) { if (xs.forEach) return xs.forEach(fn) else for (var i = 0; i < xs.length; i++) { fn(xs[i], i, xs); } }; forEach(Object_keys(Traverse.prototype), function (key) { traverse[key] = function (obj) { var args = [].slice.call(arguments, 1); var t = new Traverse(obj); return t[key].apply(t, args); }; }); substack-js-traverse-6b78600/package.json000066400000000000000000000007161173775044300203570ustar00rootroot00000000000000{ "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", "repository" : { "type" : "git", "url" : "http://github.com/substack/js-traverse.git" }, "devDependencies" : { "expresso" : "0.7.x" }, "scripts" : { "test" : "expresso" } } substack-js-traverse-6b78600/test/000077500000000000000000000000001173775044300170445ustar00rootroot00000000000000substack-js-traverse-6b78600/test/circular.js000066400000000000000000000056471173775044300212220ustar00rootroot00000000000000var assert = require('assert'); var Traverse = require('../'); var deepEqual = require('./lib/deep_equal'); var util = require('util'); exports.circular = function () { var obj = { x : 3 }; obj.y = obj; var foundY = false; Traverse(obj).forEach(function (x) { if (this.path.join('') == 'y') { assert.equal( util.inspect(this.circular.node), util.inspect(obj) ); foundY = true; } }); assert.ok(foundY); }; exports.deepCirc = function () { var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; obj.y[2] = obj; var times = 0; Traverse(obj).forEach(function (x) { if (this.circular) { assert.deepEqual(this.circular.path, []); assert.deepEqual(this.path, [ 'y', 2 ]); times ++; } }); assert.deepEqual(times, 1); }; exports.doubleCirc = function () { var obj = { x : [ 1, 2, 3 ], y : [ 4, 5 ] }; obj.y[2] = obj; obj.x.push(obj.y); var circs = []; Traverse(obj).forEach(function (x) { if (this.circular) { circs.push({ circ : this.circular, self : this, node : x }); } }); assert.deepEqual(circs[0].self.path, [ 'x', 3, 2 ]); assert.deepEqual(circs[0].circ.path, []); assert.deepEqual(circs[1].self.path, [ 'y', 2 ]); assert.deepEqual(circs[1].circ.path, []); assert.deepEqual(circs.length, 2); }; 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.deepEqual(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.deepEqual(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.deepEqual(clone.x.slice(0,3), [1,2,3]); assert.deepEqual(clone.y.slice(0,2), [4,5]); }; exports.circMapScrub = function () { var obj = { a : 1, b : 2 }; obj.c = obj; var scrubbed = Traverse(obj).map(function (node) { if (this.circular) this.remove(); }); assert.deepEqual( Object.keys(scrubbed).sort(), [ 'a', 'b' ] ); assert.ok(deepEqual(scrubbed, { a : 1, b : 2 })); assert.equal(obj.c, obj); }; substack-js-traverse-6b78600/test/date.js000066400000000000000000000014071173775044300203210ustar00rootroot00000000000000var assert = require('assert'); var Traverse = require('../'); exports.dateEach = function () { var obj = { x : new Date, y : 10, z : 5 }; var counts = {}; Traverse(obj).forEach(function (node) { var t = (node instanceof Date && 'Date') || typeof node; counts[t] = (counts[t] || 0) + 1; }); assert.deepEqual(counts, { object : 1, Date : 1, number : 2, }); }; exports.dateMap = function () { var obj = { x : new Date, y : 10, z : 5 }; var res = Traverse(obj).map(function (node) { if (typeof node === 'number') this.update(node + 100); }); assert.ok(obj.x !== res.x); assert.deepEqual(res, { x : obj.x, y : 110, z : 105, }); }; substack-js-traverse-6b78600/test/equal.js000066400000000000000000000123501173775044300205120ustar00rootroot00000000000000var assert = require('assert'); var traverse = require('../'); var deepEqual = require('./lib/deep_equal'); exports.deepDates = function () { assert.ok( deepEqual( { d : new Date, x : [ 1, 2, 3 ] }, { d : new Date, x : [ 1, 2, 3 ] } ), 'dates should be equal' ); var d0 = new Date; setTimeout(function () { assert.ok( !deepEqual( { d : d0, x : [ 1, 2, 3 ], }, { d : new Date, x : [ 1, 2, 3 ] } ), 'microseconds should count in date equality' ); }, 5); }; exports.deepCircular = function () { var a = [1]; a.push(a); // a = [ 1, *a ] var b = [1]; b.push(a); // b = [ 1, [ 1, *a ] ] assert.ok( !deepEqual(a, b), 'circular ref mount points count towards equality' ); var c = [1]; c.push(c); // c = [ 1, *c ] assert.ok( deepEqual(a, c), 'circular refs are structurally the same here' ); var d = [1]; d.push(a); // c = [ 1, [ 1, *d ] ] assert.ok( deepEqual(b, d), 'non-root circular ref structural comparison' ); }; exports.deepInstances = function () { assert.ok( !deepEqual([ new Boolean(false) ], [ false ]), 'boolean instances are not real booleans' ); assert.ok( !deepEqual([ new String('x') ], [ 'x' ]), 'string instances are not real strings' ); assert.ok( !deepEqual([ new Number(4) ], [ 4 ]), 'number instances are not real numbers' ); assert.ok( deepEqual([ new RegExp('x') ], [ /x/ ]), 'regexp instances are real regexps' ); assert.ok( !deepEqual([ new RegExp(/./) ], [ /../ ]), 'these regexps aren\'t the same' ); assert.ok( !deepEqual( [ function (x) { return x * 2 } ], [ function (x) { return x * 2 } ] ), 'functions with the same .toString() aren\'t necessarily the same' ); var f = function (x) { return x * 2 }; assert.ok( deepEqual([ f ], [ f ]), 'these functions are actually equal' ); }; exports.deepEqual = function () { assert.ok( !deepEqual([ 1, 2, 3 ], { 0 : 1, 1 : 2, 2 : 3 }), 'arrays are not objects' ); }; exports.falsy = function () { assert.ok( !deepEqual([ undefined ], [ null ]), 'null is not undefined!' ); assert.ok( !deepEqual([ null ], [ undefined ]), 'undefined is not null!' ); assert.ok( !deepEqual( { a : 1, b : 2, c : [ 3, undefined, 5 ] }, { a : 1, b : 2, c : [ 3, null, 5 ] } ), 'undefined is not null, however deeply!' ); assert.ok( !deepEqual( { a : 1, b : 2, c : [ 3, undefined, 5 ] }, { a : 1, b : 2, c : [ 3, null, 5 ] } ), 'null is not undefined, however deeply!' ); assert.ok( !deepEqual( { a : 1, b : 2, c : [ 3, undefined, 5 ] }, { a : 1, b : 2, c : [ 3, null, 5 ] } ), 'null is not undefined, however deeply!' ); }; exports.deletedArrayEqual = function () { var xs = [ 1, 2, 3, 4 ]; delete xs[2]; var ys = Object.create(Array.prototype); ys[0] = 1; ys[1] = 2; ys[3] = 4; assert.ok( deepEqual(xs, ys), 'arrays with deleted elements are only equal to' + ' arrays with similarly deleted elements' ); assert.ok( !deepEqual(xs, [ 1, 2, undefined, 4 ]), 'deleted array elements cannot be undefined' ); assert.ok( !deepEqual(xs, [ 1, 2, null, 4 ]), 'deleted array elements cannot be null' ); }; exports.deletedObjectEqual = function () { var obj = { a : 1, b : 2, c : 3 }; delete obj.c; assert.ok( deepEqual(obj, { a : 1, b : 2 }), 'deleted object elements should not show up' ); assert.ok( !deepEqual(obj, { a : 1, b : 2, c : undefined }), 'deleted object elements are not undefined' ); assert.ok( !deepEqual(obj, { a : 1, b : 2, c : null }), 'deleted object elements are not null' ); }; exports.emptyKeyEqual = function () { assert.ok(!deepEqual( { a : 1 }, { a : 1, '' : 55 } )); }; exports.deepArguments = function () { assert.ok( !deepEqual( [ 4, 5, 6 ], (function () { return arguments })(4, 5, 6) ), 'arguments are not arrays' ); assert.ok( deepEqual( (function () { return arguments })(4, 5, 6), (function () { return arguments })(4, 5, 6) ), 'arguments should equal' ); }; 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)); }; exports.deepLevels = function () { var xs = [ 1, 2, [ 3, 4, [ 5, 6 ] ] ]; assert.ok(!deepEqual(xs, [])); }; substack-js-traverse-6b78600/test/error.js000066400000000000000000000004161173775044300205340ustar00rootroot00000000000000var assert = require('assert'); var Traverse = require('../'); exports['traverse an Error'] = function () { var obj = new Error("test"); var results = Traverse(obj).map(function (node) { }); assert.deepEqual(results, { message: 'test' }); }; substack-js-traverse-6b78600/test/has.js000066400000000000000000000007441173775044300201620ustar00rootroot00000000000000var assert = require('assert'); var traverse = require('../'); exports.has = function () { 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) }; substack-js-traverse-6b78600/test/instance.js000066400000000000000000000006771173775044300212200ustar00rootroot00000000000000var assert = require('assert'); var Traverse = require('../'); var EventEmitter = require('events').EventEmitter; exports['check instanceof on node elems'] = function () { var counts = { emitter : 0 }; Traverse([ new EventEmitter, 3, 4, { ev : new EventEmitter }]) .forEach(function (node) { if (node instanceof EventEmitter) counts.emitter ++; }) ; assert.equal(counts.emitter, 2); }; substack-js-traverse-6b78600/test/interface.js000066400000000000000000000020021173775044300213340ustar00rootroot00000000000000var assert = require('assert'); var Traverse = require('../'); exports['interface map'] = function () { var obj = { a : [ 5,6,7 ], b : { c : [8] } }; assert.deepEqual( Traverse.paths(obj) .sort() .map(function (path) { return path.join('/') }) .slice(1) .join(' ') , 'a a/0 a/1 a/2 b b/c b/c/0' ); assert.deepEqual( Traverse.nodes(obj), [ { a: [ 5, 6, 7 ], b: { c: [ 8 ] } }, [ 5, 6, 7 ], 5, 6, 7, { c: [ 8 ] }, [ 8 ], 8 ] ); assert.deepEqual( Traverse.map(obj, function (node) { if (typeof node == 'number') { return node + 1000; } else if (Array.isArray(node)) { return node.join(' '); } }), { a: '5 6 7', b: { c: '8' } } ); var nodes = 0; Traverse.forEach(obj, function (node) { nodes ++ }); assert.deepEqual(nodes, 8); }; substack-js-traverse-6b78600/test/json.js000066400000000000000000000023531173775044300203560ustar00rootroot00000000000000var assert = require('assert'); var Traverse = require('../'); exports['json test'] = function () { var id = 54; var callbacks = {}; var obj = { moo : function () {}, foo : [2,3,4, function () {}] }; var scrubbed = Traverse(obj).map(function (x) { if (typeof x === 'function') { callbacks[id] = { id : id, f : x, path : this.path }; this.update('[Function]'); id++; } }); assert.equal( scrubbed.moo, '[Function]', 'obj.moo replaced with "[Function]"' ); assert.equal( scrubbed.foo[3], '[Function]', 'obj.foo[3] replaced with "[Function]"' ); assert.deepEqual(scrubbed, { moo : '[Function]', foo : [ 2, 3, 4, "[Function]" ] }, 'Full JSON string matches'); assert.deepEqual( typeof obj.moo, 'function', 'Original obj.moo still a function' ); assert.deepEqual( typeof obj.foo[3], 'function', 'Original obj.foo[3] still a function' ); assert.deepEqual(callbacks, { 54: { id: 54, f : obj.moo, path: [ 'moo' ] }, 55: { id: 55, f : obj.foo[3], path: [ 'foo', '3' ] }, }, 'Check the generated callbacks list'); }; substack-js-traverse-6b78600/test/keys.js000066400000000000000000000014071173775044300203570ustar00rootroot00000000000000var assert = require('assert'); var Traverse = require('../'); exports['sort test'] = function () { var acc = []; Traverse({ a: 30, b: 22, id: 9 }).forEach(function (node) { if ((! Array.isArray(node)) && typeof node === 'object') { this.before(function(node) { this.keys = Object.keys(node); this.keys.sort(function(a, b) { a = [a === "id" ? 0 : 1, a]; b = [b === "id" ? 0 : 1, b]; return a < b ? -1 : a > b ? 1 : 0; }); }); } if (this.isLeaf) acc.push(node); }); assert.equal( acc.join(' '), '9 30 22', 'Traversal in a custom order' ); }; substack-js-traverse-6b78600/test/leaves.js000066400000000000000000000006541173775044300206660ustar00rootroot00000000000000var assert = require('assert'); var Traverse = require('../'); exports['leaves test'] = function () { var acc = []; Traverse({ a : [1,2,3], b : 4, c : [5,6], d : { e : [7,8], f : 9 } }).forEach(function (x) { if (this.isLeaf) acc.push(x); }); assert.equal( acc.join(' '), '1 2 3 4 5 6 7 8 9', 'Traversal in the right(?) order' ); }; substack-js-traverse-6b78600/test/lib/000077500000000000000000000000001173775044300176125ustar00rootroot00000000000000substack-js-traverse-6b78600/test/lib/deep_equal.js000066400000000000000000000055041173775044300222600ustar00rootroot00000000000000var traverse = require('../../'); module.exports = function (a, b) { if (arguments.length !== 2) { throw new Error( 'deepEqual requires exactly two objects to compare against' ); } var equal = true; var node = b; traverse(a).forEach(function (y) { var notEqual = (function () { equal = false; //this.stop(); return undefined; }).bind(this); //if (node === undefined || node === null) return notEqual(); if (!this.isRoot) { /* if (!Object.hasOwnProperty.call(node, this.key)) { return notEqual(); } */ if (typeof node !== 'object') return notEqual(); 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(b).get(this.circular.path) !== x) notEqual(); } else if (typeof x !== typeof y) { notEqual(); } else if (x === null || y === null || x === undefined || y === undefined) { if (x !== 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 (toS(y) === '[object Arguments]' || toS(x) === '[object Arguments]') { if (toS(x) !== toS(y)) { notEqual(); } } else if (toS(y) === '[object RegExp]' || toS(x) === '[object RegExp]') { if (!x || !y || x.toString() !== y.toString()) 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 notEqual(); for (var i = 0; i < kx.length; i++) { var k = kx[i]; if (!Object.hasOwnProperty.call(y, k)) { notEqual(); } } } } }); return equal; }; substack-js-traverse-6b78600/test/mutability.js000066400000000000000000000144471173775044300215770ustar00rootroot00000000000000var assert = require('assert'); var Traverse = require('../'); var deepEqual = require('./lib/deep_equal'); exports.mutate = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; var res = Traverse(obj).forEach(function (x) { if (typeof x === 'number' && x % 2 === 0) { this.update(x * 10); } }); assert.deepEqual(obj, res); assert.deepEqual(obj, { a : 1, b : 20, c : [ 3, 40 ] }); }; exports.mutateT = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; var res = Traverse.forEach(obj, function (x) { if (typeof x === 'number' && x % 2 === 0) { this.update(x * 10); } }); assert.deepEqual(obj, res); assert.deepEqual(obj, { a : 1, b : 20, c : [ 3, 40 ] }); }; exports.map = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; var res = Traverse(obj).map(function (x) { if (typeof x === 'number' && x % 2 === 0) { this.update(x * 10); } }); assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] }); assert.deepEqual(res, { a : 1, b : 20, c : [ 3, 40 ] }); }; exports.mapT = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; var res = Traverse.map(obj, function (x) { if (typeof x === 'number' && x % 2 === 0) { this.update(x * 10); } }); assert.deepEqual(obj, { a : 1, b : 2, c : [ 3, 4 ] }); assert.deepEqual(res, { a : 1, b : 20, c : [ 3, 40 ] }); }; exports.clone = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; var res = Traverse(obj).clone(); assert.deepEqual(obj, res); assert.ok(obj !== res); obj.a ++; assert.deepEqual(res.a, 1); obj.c.push(5); assert.deepEqual(res.c, [ 3, 4 ]); }; exports.cloneT = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; var res = Traverse.clone(obj); assert.deepEqual(obj, res); assert.ok(obj !== res); obj.a ++; assert.deepEqual(res.a, 1); obj.c.push(5); assert.deepEqual(res.c, [ 3, 4 ]); }; exports.reduce = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; 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 ]); }; exports.reduceInit = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; 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); }; exports.remove = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; Traverse(obj).forEach(function (x) { if (this.isLeaf && x % 2 == 0) this.remove(); }); assert.deepEqual(obj, { a : 1, c : [ 3 ] }); }; exports.removeNoStop = function() { var obj = { a : 1, b : 2, c : { d: 3, e: 4 }, f: 5 }; var keys = []; Traverse(obj).forEach(function (x) { keys.push(this.key) if (this.key == 'c') this.remove(); }); assert.deepEqual(keys, [undefined, 'a', 'b', 'c', 'd', 'e', 'f']) } exports.removeStop = function() { var obj = { a : 1, b : 2, c : { d: 3, e: 4 }, f: 5 }; var keys = []; Traverse(obj).forEach(function (x) { keys.push(this.key) if (this.key == 'c') this.remove(true); }); assert.deepEqual(keys, [undefined, 'a', 'b', 'c', 'f']) } exports.removeMap = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; 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 ] }); }; exports.delete = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; Traverse(obj).forEach(function (x) { if (this.isLeaf && x % 2 == 0) this.delete(); }); assert.ok(!deepEqual( obj, { a : 1, c : [ 3, undefined ] } )); assert.ok(deepEqual( obj, { a : 1, c : [ 3 ] } )); assert.ok(!deepEqual( obj, { a : 1, c : [ 3, null ] } )); }; exports.deleteNoStop = function() { var obj = { a : 1, b : 2, c : { d: 3, e: 4 } }; var keys = []; Traverse(obj).forEach(function (x) { keys.push(this.key) if (this.key == 'c') this.delete(); }); assert.deepEqual(keys, [undefined, 'a', 'b', 'c', 'd', 'e']) } exports.deleteStop = function() { var obj = { a : 1, b : 2, c : { d: 3, e: 4 } }; var keys = []; Traverse(obj).forEach(function (x) { keys.push(this.key) if (this.key == 'c') this.delete(true); }); assert.deepEqual(keys, [undefined, 'a', 'b', 'c']) } exports.deleteRedux = function () { var obj = { a : 1, b : 2, c : [ 3, 4, 5 ] }; Traverse(obj).forEach(function (x) { if (this.isLeaf && x % 2 == 0) this.delete(); }); assert.ok(!deepEqual( obj, { a : 1, c : [ 3, undefined, 5 ] } )); assert.ok(deepEqual( obj, { a : 1, c : [ 3 ,, 5 ] } )); assert.ok(!deepEqual( obj, { a : 1, c : [ 3, null, 5 ] } )); assert.ok(!deepEqual( obj, { a : 1, c : [ 3, 5 ] } )); }; exports.deleteMap = function () { var obj = { a : 1, b : 2, c : [ 3, 4 ] }; var res = Traverse(obj).map(function (x) { if (this.isLeaf && x % 2 == 0) this.delete(); }); assert.ok(deepEqual( obj, { a : 1, b : 2, c : [ 3, 4 ] } )); var xs = [ 3, 4 ]; delete xs[1]; assert.ok(deepEqual( res, { a : 1, c : xs } )); assert.ok(deepEqual( res, { a : 1, c : [ 3, ] } )); assert.ok(deepEqual( res, { a : 1, c : [ 3 ] } )); }; exports.deleteMapRedux = function () { var obj = { a : 1, b : 2, c : [ 3, 4, 5 ] }; var res = Traverse(obj).map(function (x) { if (this.isLeaf && x % 2 == 0) this.delete(); }); assert.ok(deepEqual( obj, { a : 1, b : 2, c : [ 3, 4, 5 ] } )); var xs = [ 3, 4, 5 ]; delete xs[1]; assert.ok(deepEqual( res, { a : 1, c : xs } )); assert.ok(!deepEqual( res, { a : 1, c : [ 3, 5 ] } )); assert.ok(deepEqual( res, { a : 1, c : [ 3 ,, 5 ] } )); }; substack-js-traverse-6b78600/test/negative.js000066400000000000000000000010471173775044300212060ustar00rootroot00000000000000var Traverse = require('../'); var assert = require('assert'); exports['negative update test'] = function () { var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ]; var fixed = Traverse.map(obj, function (x) { if (x < 0) this.update(x + 128); }); assert.deepEqual(fixed, [ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ], 'Negative values += 128' ); assert.deepEqual(obj, [ 5, 6, -3, [ 7, 8, -2, 1 ], { f: 10, g: -13 } ], 'Original references not modified' ); } substack-js-traverse-6b78600/test/obj.js000066400000000000000000000005251173775044300201560ustar00rootroot00000000000000var assert = require('assert'); var Traverse = require('../'); exports['traverse an object with nested functions'] = function () { var to = setTimeout(function () { assert.fail('never ran'); }, 1000); function Cons (x) { clearTimeout(to); assert.equal(x, 10); }; Traverse(new Cons(10)); }; substack-js-traverse-6b78600/test/siblings.js000066400000000000000000000022351173775044300212160ustar00rootroot00000000000000var assert = require('assert'); var traverse = require('../'); exports.siblings = function () { var obj = { a : 1, b : 2, c : [ 4, 5, 6 ] }; var res = traverse(obj).reduce(function (acc, x) { var p = '/' + this.path.join('/'); if (this.parent) { acc[p] = { siblings : this.parent.keys, key : this.key, index : this.parent.keys.indexOf(this.key) }; } else { acc[p] = { siblings : [], key : this.key, index : -1 } } return acc; }, {}); assert.deepEqual(res, { '/' : { siblings : [], key : undefined, index : -1 }, '/a' : { siblings : [ 'a', 'b', 'c' ], key : 'a', index : 0 }, '/b' : { siblings : [ 'a', 'b', 'c' ], key : 'b', index : 1 }, '/c' : { siblings : [ 'a', 'b', 'c' ], key : 'c', index : 2 }, '/c/0' : { siblings : [ '0', '1', '2' ], key : '0', index : 0 }, '/c/1' : { siblings : [ '0', '1', '2' ], key : '1', index : 1 }, '/c/2' : { siblings : [ '0', '1', '2' ], key : '2', index : 2 } }); }; substack-js-traverse-6b78600/test/stop.js000066400000000000000000000017311173775044300203710ustar00rootroot00000000000000var assert = require('assert'); var traverse = require('../'); exports.stop = function () { var visits = 0; traverse('abcdefghij'.split('')).forEach(function (node) { if (typeof node === 'string') { visits ++; if (node === 'e') this.stop() } }); assert.equal(visits, 5); }; exports.stopMap = function () { var s = traverse('abcdefghij'.split('')).map(function (node) { if (typeof node === 'string') { if (node === 'e') this.stop() return node.toUpperCase(); } }).join(''); assert.equal(s, 'ABCDEfghij'); }; exports.stopReduce = function () { var obj = { a : [ 4, 5 ], b : [ 6, [ 7, 8, 9 ] ] }; var xs = traverse(obj).reduce(function (acc, node) { if (this.isLeaf) { if (node === 7) this.stop(); else acc.push(node) } return acc; }, []); assert.deepEqual(xs, [ 4, 5, 6 ]); }; substack-js-traverse-6b78600/test/stringify.js000066400000000000000000000020111173775044300214120ustar00rootroot00000000000000var assert = require('assert'); var Traverse = require('../'); exports.stringify = function () { var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ]; var s = ''; Traverse(obj).forEach(function (node) { if (Array.isArray(node)) { this.before(function () { s += '[' }); this.post(function (child) { if (!child.isLast) s += ','; }); this.after(function () { s += ']' }); } else if (typeof node == 'object') { this.before(function () { s += '{' }); this.pre(function (x, key) { s += '"' + key + '"' + ':'; }); this.post(function (child) { if (!child.isLast) s += ','; }); this.after(function () { s += '}' }); } else if (typeof node == 'function') { s += 'null'; } else { s += node.toString(); } }); assert.equal(s, JSON.stringify(obj)); } substack-js-traverse-6b78600/test/subexpr.js000066400000000000000000000015511173775044300210740ustar00rootroot00000000000000var traverse = require('../'); var assert = require('assert'); exports.subexpr = function () { var obj = [ 'a', 4, 'b', 5, 'c', 6 ]; var r = traverse(obj).map(function (x) { if (typeof x === 'number') { this.update([ x - 0.1, x, x + 0.1 ], true); } }); assert.deepEqual(obj, [ 'a', 4, 'b', 5, 'c', 6 ]); assert.deepEqual(r, [ 'a', [ 3.9, 4, 4.1 ], 'b', [ 4.9, 5, 5.1 ], 'c', [ 5.9, 6, 6.1 ], ]); }; exports.block = function () { var obj = [ [ 1 ], [ 2 ], [ 3 ] ]; var r = traverse(obj).map(function (x) { if (Array.isArray(x) && !this.isRoot) { if (x[0] === 5) this.block() else this.update([ [ x[0] + 1 ] ]) } }); assert.deepEqual(r, [ [ [ [ [ [ 5 ] ] ] ] ], [ [ [ [ 5 ] ] ] ], [ [ [ 5 ] ] ], ]); }; substack-js-traverse-6b78600/test/super_deep.js000066400000000000000000000016411173775044300215370ustar00rootroot00000000000000var assert = require('assert'); var traverse = require('../'); var deepEqual = require('./lib/deep_equal'); exports.super_deep = function () { var util = require('util'); var a0 = make(); var a1 = make(); assert.ok(deepEqual(a0, a1)); a0.c.d.moo = true; assert.ok(!deepEqual(a0, a1)); a1.c.d.moo = true; assert.ok(deepEqual(a0, a1)); // TODO: this one //a0.c.a = a1; //assert.ok(!deepEqual(a0, a1)); }; function make () { var a = { self : 'a' }; var b = { self : 'b' }; var c = { self : 'c' }; var d = { self : 'd' }; var e = { self : 'e' }; a.a = a; a.b = b; a.c = c; b.a = a; b.b = b; b.c = c; c.a = a; c.b = b; c.c = c; c.d = d; d.a = a; d.b = b; d.c = c; d.d = d; d.e = e; e.a = a; e.b = b; e.c = c; e.d = d; e.e = e; return a; }