[Pkg-javascript-commits] [node-traverse] 02/06: New upstream version 0.6.6
Praveen Arimbrathodiyil
praveen at moszumanska.debian.org
Tue Sep 19 14:16:00 UTC 2017
This is an automated email from the git hooks/post-receive script.
praveen pushed a commit to branch master
in repository node-traverse.
commit df5a8319258fde64b6f62bafc2a0e2ac42e29ed2
Author: Pirate Praveen <praveen at debian.org>
Date: Tue Sep 19 19:35:50 2017 +0530
New upstream version 0.6.6
---
.travis.yml | 3 +
index.js | 77 +++++++-----
package.json | 50 ++++++--
README.markdown => readme.markdown | 123 ++++++-------------
test/circular.js | 100 ++++++++--------
test/date.js | 24 ++--
test/equal.js | 136 ++++++++++++---------
test/error.js | 18 ++-
test/has.js | 20 ++--
test/instance.js | 16 +--
test/interface.js | 27 +++--
test/json.js | 24 ++--
test/keys.js | 14 ++-
test/leaves.js | 15 +--
test/mutability.js | 236 ++++++++++++++++++++++---------------
test/negative.js | 17 +--
test/obj.js | 18 ++-
test/siblings.js | 10 +-
test/stop.js | 23 ++--
test/stringify.js | 14 +--
test/subexpr.js | 18 +--
test/super_deep.js | 15 +--
22 files changed, 542 insertions(+), 456 deletions(-)
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..2d26206
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 0.6
diff --git a/index.js b/index.js
index b629542..2f2cf67 100644
--- a/index.js
+++ b/index.js
@@ -10,7 +10,7 @@ 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)) {
+ if (!node || !hasOwnProperty.call(node, key)) {
node = undefined;
break;
}
@@ -23,7 +23,7 @@ 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)) {
+ if (!node || !hasOwnProperty.call(node, key)) {
return false;
}
node = node[key];
@@ -35,7 +35,7 @@ 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] = {};
+ if (!hasOwnProperty.call(node, key)) node[key] = {};
node = node[key];
}
node[ps[i]] = value;
@@ -94,7 +94,7 @@ Traverse.prototype.clone = function () {
parents.push(src);
nodes.push(dst);
- forEach(Object_keys(src), function (key) {
+ forEach(objectKeys(src), function (key) {
dst[key] = clone(src[key]);
});
@@ -141,7 +141,7 @@ function walk (root, cb, immutable) {
if (stopHere) keepGoing = false;
},
remove : function (stopHere) {
- if (Array_isArray(state.parent.node)) {
+ if (isArray(state.parent.node)) {
state.parent.node.splice(state.key, 1);
}
else {
@@ -160,24 +160,31 @@ function walk (root, cb, immutable) {
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;
+ function updateState() {
+ if (typeof state.node === 'object' && state.node !== null) {
+ if (!state.keys || state.node_ !== state.node) {
+ state.keys = objectKeys(state.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;
+ else {
+ state.isLeaf = true;
+ state.keys = null;
+ }
+
+ state.notLeaf = !state.isLeaf;
+ state.notRoot = !state.isRoot;
}
- state.notLeaf = !state.isLeaf;
- state.notRoot = !state.isRoot;
+ updateState();
// use return values to update if defined
var ret = cb.call(state, state.node);
@@ -191,13 +198,15 @@ function walk (root, cb, immutable) {
&& state.node !== null && !state.circular) {
parents.push(state);
+ updateState();
+
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)) {
+ if (immutable && hasOwnProperty.call(state.node, key)) {
state.node[key] = child.node;
}
@@ -221,11 +230,11 @@ function copy (src) {
if (typeof src === 'object' && src !== null) {
var dst;
- if (Array_isArray(src)) {
+ if (isArray(src)) {
dst = [];
}
else if (isDate(src)) {
- dst = new Date(src);
+ dst = new Date(src.getTime ? src.getTime() : src);
}
else if (isRegExp(src)) {
dst = new RegExp(src);
@@ -245,15 +254,21 @@ function copy (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 || {};
+ 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;
}
- forEach(Object_keys(src), function (key) {
+ forEach(objectKeys(src), function (key) {
dst[key] = src[key];
});
return dst;
@@ -261,7 +276,7 @@ function copy (src) {
else return src;
}
-var Object_keys = Object.keys || function keys (obj) {
+var objectKeys = Object.keys || function keys (obj) {
var res = [];
for (var key in obj) res.push(key)
return res;
@@ -275,7 +290,7 @@ 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) {
+var isArray = Array.isArray || function isArray (xs) {
return Object.prototype.toString.call(xs) === '[object Array]';
};
@@ -286,10 +301,14 @@ var forEach = function (xs, fn) {
}
};
-forEach(Object_keys(Traverse.prototype), function (key) {
+forEach(objectKeys(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);
};
});
+
+var hasOwnProperty = Object.hasOwnProperty || function (obj, key) {
+ return key in obj;
+};
diff --git a/package.json b/package.json
index d804fce..ed9a287 100644
--- a/package.json
+++ b/package.json
@@ -1,18 +1,46 @@
{
"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"
+ "version" : "0.6.6",
+ "description" : "traverse and transform objects by visiting every node on a recursive walk",
+ "main" : "index.js",
+ "directories" : {
+ "example" : "example",
+ "test" : "test"
},
"devDependencies" : {
- "expresso" : "0.7.x"
+ "tape" : "~1.0.4"
},
"scripts" : {
- "test" : "expresso"
- }
+ "test" : "tape test/*.js"
+ },
+ "testling" : {
+ "files" : "test/*.js",
+ "browsers" : {
+ "iexplore" : [ "6.0", "7.0", "8.0", "9.0" ],
+ "chrome" : [ "10.0", "20.0" ],
+ "firefox" : [ "10.0", "15.0" ],
+ "safari" : [ "5.1" ],
+ "opera" : [ "12.0" ]
+ }
+ },
+ "repository" : {
+ "type" : "git",
+ "url" : "git://github.com/substack/js-traverse.git"
+ },
+ "homepage" : "https://github.com/substack/js-traverse",
+ "keywords" : [
+ "traverse",
+ "walk",
+ "recursive",
+ "map",
+ "forEach",
+ "deep",
+ "clone"
+ ],
+ "author" : {
+ "name" : "James Halliday",
+ "email" : "mail at substack.net",
+ "url" : "http://substack.net"
+ },
+ "license" : "MIT"
}
diff --git a/README.markdown b/readme.markdown
similarity index 71%
rename from README.markdown
rename to readme.markdown
index 75a0ec1..fbfd06e 100644
--- a/README.markdown
+++ b/readme.markdown
@@ -1,13 +1,14 @@
-traverse
-========
+# traverse
Traverse and transform objects by visiting every node on a recursive walk.
-examples
-========
+[![browser support](http://ci.testling.com/substack/js-traverse.png)](http://ci.testling.com/substack/js-traverse)
-transform negative numbers in-place
------------------------------------
+[![build status](https://secure.travis-ci.org/substack/js-traverse.png)](http://travis-ci.org/substack/js-traverse)
+
+# examples
+
+## transform negative numbers in-place
negative.js
@@ -26,8 +27,7 @@ Output:
[ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ]
-collect leaf nodes
-------------------
+## collect leaf nodes
leaves.js
@@ -53,8 +53,7 @@ Output:
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
-scrub circular references
--------------------------
+## scrub circular references
scrub.js:
@@ -74,26 +73,22 @@ output:
{ a: 1, b: 2, c: [ 3, 4 ] }
-methods
-=======
+# methods
Each method that takes an `fn` uses the context documented below in the context
section.
-.map(fn)
---------
+## .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)
-------------
+## .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)
-----------------
+## .reduce(fn, acc)
For each node in the object, perform a
[left-fold](http://en.wikipedia.org/wiki/Fold_(higher-order_function))
@@ -102,155 +97,113 @@ 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()
---------
+## .paths()
Return an `Array` of every possible non-cyclic path in the object.
Paths are `Array`s of string keys.
-.nodes()
---------
+## .nodes()
Return an `Array` of every node in the object.
-.clone()
---------
+## .clone()
Create a deep clone of the object.
-.get(path)
-----------
+## .get(path)
Get the element at the array `path`.
-.set(path, value)
------------------
+## .set(path, value)
Set the element at the array `path` to `value`.
-.has(path)
-----------
+## .has(path)
Return whether the element at the array `path` exists.
-context
-=======
+# context
Each method that takes a callback has a context (its `this` object) with these
attributes:
-this.node
----------
+## this.node
The present node on the recursive walk
-this.path
----------
+## this.path
An array of string keys from the root to the present node
-this.parent
------------
+## this.parent
The context of the node's parent.
This is `undefined` for the root node.
-this.key
---------
+## 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
--------------------------
+## this.isRoot, this.notRoot
Whether the present node is the root node
-this.isLeaf, this.notLeaf
--------------------------
+## this.isLeaf, this.notLeaf
Whether or not the present node is a leaf node (has no children)
-this.level
-----------
+## this.level
Depth of the node within the traversal
-this.circular
--------------
+## 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)
-----------------------------------
+## 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)
--------------
+## 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)
--------------
+## this.delete(stopHere=false)
Delete the current element from its parent in the output. Calls `delete` even on
Arrays.
-this.before(fn)
----------------
+## 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)
---------------
+## this.after(fn)
Call this function after any of the children are traversed.
-this.pre(fn)
-------------
+## this.pre(fn)
Call this function before each of the children are traversed.
-this.post(fn)
--------------
+## this.post(fn)
Call this function after each of the children are traversed.
-install
-=======
+# 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:
+# license
-* Internet Explorer 5.5, 6.0, 7.0, 8.0, 9.0
-* Firefox 3.5
-* Chrome 6.0
-* Opera 10.6
-* Safari 5.0
+MIT
diff --git a/test/circular.js b/test/circular.js
index 9162601..f56506a 100644
--- a/test/circular.js
+++ b/test/circular.js
@@ -1,115 +1,117 @@
-var assert = require('assert');
-var Traverse = require('../');
+var test = require('tape');
+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 ] };
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 ] };
obj.y[2] = obj;
obj.x.push(obj.y);
var circs = [];
- Traverse(obj).forEach(function (x) {
+ 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, []);
+ 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 ] };
obj.y[2] = obj;
obj.x.push(obj.y);
- 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 ] };
obj.y[2] = obj;
obj.x.push(obj.y);
- var c = Traverse(obj).map(function (x) {
+ var c = traverse(obj).map(function (x) {
if (this.circular) {
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 ] };
obj.y[2] = obj;
obj.x.push(obj.y);
- 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();
+});
diff --git a/test/date.js b/test/date.js
index 4ca06dc..54db4b0 100644
--- a/test/date.js
+++ b/test/date.js
@@ -1,35 +1,37 @@
-var assert = require('assert');
-var Traverse = require('../');
+var test = require('tape');
+var traverse = require('../');
-exports.dateEach = function () {
+test('dateEach', function (t) {
var obj = { x : new Date, y : 10, z : 5 };
var counts = {};
- Traverse(obj).forEach(function (node) {
+ traverse(obj).forEach(function (node) {
var t = (node instanceof Date && 'Date') || typeof node;
counts[t] = (counts[t] || 0) + 1;
});
- assert.deepEqual(counts, {
+ t.same(counts, {
object : 1,
Date : 1,
number : 2,
});
-};
+ 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,
y : 110,
z : 105,
});
-};
+ t.end();
+});
diff --git a/test/equal.js b/test/equal.js
index decc755..fd0463c 100644
--- a/test/equal.js
+++ b/test/equal.js
@@ -1,9 +1,11 @@
-var assert = require('assert');
+var test = require('tape');
var traverse = require('../');
var deepEqual = require('./lib/deep_equal');
-exports.deepDates = function () {
- assert.ok(
+test('deepDates', function (t) {
+ t.plan(2);
+
+ t.ok(
deepEqual(
{ d : new Date, x : [ 1, 2, 3 ] },
{ d : new Date, x : [ 1, 2, 3 ] }
@@ -13,7 +15,7 @@ exports.deepDates = function () {
var d0 = new Date;
setTimeout(function () {
- assert.ok(
+ t.ok(
!deepEqual(
{ d : d0, x : [ 1, 2, 3 ], },
{ d : new Date, x : [ 1, 2, 3 ] }
@@ -21,62 +23,64 @@ exports.deepDates = function () {
'microseconds should count in date equality'
);
}, 5);
-};
+});
-exports.deepCircular = function () {
+test('deepCircular', function (t) {
var a = [1];
a.push(a); // a = [ 1, *a ]
var b = [1];
b.push(a); // b = [ 1, [ 1, *a ] ]
- assert.ok(
+ t.ok(
!deepEqual(a, b),
'circular ref mount points count towards equality'
);
var c = [1];
c.push(c); // c = [ 1, *c ]
- assert.ok(
+ t.ok(
deepEqual(a, c),
'circular refs are structurally the same here'
);
var d = [1];
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 ]),
'boolean instances are not real booleans'
);
- assert.ok(
+ t.ok(
!deepEqual([ new String('x') ], [ 'x' ]),
'string instances are not real strings'
);
- assert.ok(
+ t.ok(
!deepEqual([ new Number(4) ], [ 4 ]),
'number instances are not real numbers'
);
- assert.ok(
+ t.ok(
deepEqual([ new RegExp('x') ], [ /x/ ]),
'regexp instances are real regexps'
);
- assert.ok(
+ t.ok(
!deepEqual([ new RegExp(/./) ], [ /../ ]),
'these regexps aren\'t the same'
);
- assert.ok(
+ t.ok(
!deepEqual(
[ function (x) { return x * 2 } ],
[ function (x) { return x * 2 } ]
@@ -85,31 +89,34 @@ exports.deepInstances = function () {
);
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 ]),
'null is not undefined!'
);
- assert.ok(
+ t.ok(
!deepEqual([ null ], [ undefined ]),
'undefined is not null!'
);
- assert.ok(
+ t.ok(
!deepEqual(
{ a : 1, b : 2, c : [ 3, undefined, 5 ] },
{ a : 1, b : 2, c : [ 3, null, 5 ] }
@@ -117,7 +124,7 @@ exports.falsy = function () {
'undefined is not null, however deeply!'
);
- assert.ok(
+ t.ok(
!deepEqual(
{ a : 1, b : 2, c : [ 3, undefined, 5 ] },
{ a : 1, b : 2, c : [ 3, null, 5 ] }
@@ -125,16 +132,18 @@ exports.falsy = function () {
'null is not undefined, however deeply!'
);
- assert.ok(
+ t.ok(
!deepEqual(
{ a : 1, b : 2, c : [ 3, undefined, 5 ] },
{ a : 1, b : 2, c : [ 3, null, 5 ] }
),
'null is not undefined, however deeply!'
);
-};
+
+ t.end();
+});
-exports.deletedArrayEqual = function () {
+test('deletedArrayEqual', function (t) {
var xs = [ 1, 2, 3, 4 ];
delete xs[2];
@@ -143,51 +152,57 @@ exports.deletedArrayEqual = function () {
ys[1] = 2;
ys[3] = 4;
- assert.ok(
+ t.ok(
deepEqual(xs, ys),
'arrays with deleted elements are only equal to'
+ ' arrays with similarly deleted elements'
);
- assert.ok(
+ t.ok(
!deepEqual(xs, [ 1, 2, undefined, 4 ]),
'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 }),
'deleted object elements should not show up'
);
- assert.ok(
+ t.ok(
!deepEqual(obj, { a : 1, b : 2, c : undefined }),
'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(
[ 4, 5, 6 ],
(function () { return arguments })(4, 5, 6)
@@ -195,26 +210,31 @@ exports.deepArguments = function () {
'arguments are not arrays'
);
- assert.ok(
+ t.ok(
deepEqual(
(function () { return arguments })(4, 5, 6),
(function () { return arguments })(4, 5, 6)
),
'arguments should equal'
);
-};
+
+ 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();
+});
diff --git a/test/error.js b/test/error.js
index 30e37d2..447c725 100644
--- a/test/error.js
+++ b/test/error.js
@@ -1,13 +1,11 @@
-var assert = require('assert');
-var Traverse = require('../');
+var test = require('tape');
+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) { });
-
- assert.deepEqual(results, {
- message: 'test'
- });
-};
+ var results = traverse(obj).map(function (node) {});
+ t.same(results, { message: 'test' });
+
+ t.end();
+});
diff --git a/test/has.js b/test/has.js
index af0c83c..94a50c6 100644
--- a/test/has.js
+++ b/test/has.js
@@ -1,13 +1,15 @@
-var assert = require('assert');
+var test = require('tape');
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();
+});
diff --git a/test/instance.js b/test/instance.js
index 8d73525..112f477 100644
--- a/test/instance.js
+++ b/test/instance.js
@@ -1,17 +1,17 @@
-var assert = require('assert');
-var Traverse = require('../');
+var test = require('tape');
+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) {
if (node instanceof EventEmitter) counts.emitter ++;
})
;
- assert.equal(counts.emitter, 2);
-};
-
+ t.equal(counts.emitter, 2);
+
+ t.end();
+});
diff --git a/test/interface.js b/test/interface.js
index fce5bf9..f454c27 100644
--- a/test/interface.js
+++ b/test/interface.js
@@ -1,11 +1,11 @@
-var assert = require('assert');
-var Traverse = require('../');
+var test = require('tape');
+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()
.map(function (path) { return path.join('/') })
.slice(1)
@@ -14,8 +14,8 @@ exports['interface map'] = function () {
'a a/0 a/1 a/2 b b/c b/c/0'
);
- assert.deepEqual(
- Traverse.nodes(obj),
+ t.same(
+ traverse.nodes(obj),
[
{ a: [ 5, 6, 7 ], b: { c: [ 8 ] } },
[ 5, 6, 7 ], 5, 6, 7,
@@ -23,8 +23,8 @@ exports['interface map'] = function () {
]
);
- assert.deepEqual(
- Traverse.map(obj, function (node) {
+ t.same(
+ traverse.map(obj, function (node) {
if (typeof node == 'number') {
return node + 1000;
}
@@ -36,7 +36,8 @@ exports['interface map'] = function () {
);
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();
+});
diff --git a/test/json.js b/test/json.js
index 0a04529..46d55e6 100644
--- a/test/json.js
+++ b/test/json.js
@@ -1,12 +1,12 @@
-var assert = require('assert');
-var Traverse = require('../');
+var test = require('tape');
+var traverse = require('../');
-exports['json test'] = function () {
+test('json test', function (t) {
var id = 54;
var callbacks = {};
var obj = { moo : function () {}, foo : [2,3,4, function () {}] };
- var scrubbed = Traverse(obj).map(function (x) {
+ var scrubbed = traverse(obj).map(function (x) {
if (typeof x === 'function') {
callbacks[id] = { id : id, f : x, path : this.path };
this.update('[Function]');
@@ -14,34 +14,36 @@ exports['json test'] = function () {
}
});
- assert.equal(
+ t.equal(
scrubbed.moo, '[Function]',
'obj.moo replaced with "[Function]"'
);
- assert.equal(
+ t.equal(
scrubbed.foo[3], '[Function]',
'obj.foo[3] replaced with "[Function]"'
);
- assert.deepEqual(scrubbed, {
+ t.same(scrubbed, {
moo : '[Function]',
foo : [ 2, 3, 4, "[Function]" ]
}, 'Full JSON string matches');
- assert.deepEqual(
+ t.same(
typeof obj.moo, 'function',
'Original obj.moo still a function'
);
- assert.deepEqual(
+ t.same(
typeof obj.foo[3], 'function',
'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();
+});
diff --git a/test/keys.js b/test/keys.js
index 7ecd545..9661140 100644
--- a/test/keys.js
+++ b/test/keys.js
@@ -1,9 +1,9 @@
-var assert = require('assert');
-var Traverse = require('../');
+var test = require('tape');
+var traverse = require('../');
-exports['sort test'] = function () {
+test('sort test', function (t) {
var acc = [];
- Traverse({
+ traverse({
a: 30,
b: 22,
id: 9
@@ -21,9 +21,11 @@ exports['sort test'] = function () {
if (this.isLeaf) acc.push(node);
});
- assert.equal(
+ t.equal(
acc.join(' '),
'9 30 22',
'Traversal in a custom order'
);
-};
+
+ t.end();
+});
diff --git a/test/leaves.js b/test/leaves.js
index e520b72..c04ad5f 100644
--- a/test/leaves.js
+++ b/test/leaves.js
@@ -1,9 +1,9 @@
-var assert = require('assert');
-var Traverse = require('../');
+var test = require('tape');
+var traverse = require('../');
-exports['leaves test'] = function () {
+test('leaves test', function (t) {
var acc = [];
- Traverse({
+ traverse({
a : [1,2,3],
b : 4,
c : [5,6],
@@ -12,10 +12,11 @@ exports['leaves test'] = function () {
if (this.isLeaf) acc.push(x);
});
- assert.equal(
+ t.equal(
acc.join(' '),
'1 2 3 4 5 6 7 8 9',
'Traversal in the right(?) order'
);
-};
-
+
+ t.end();
+});
diff --git a/test/mutability.js b/test/mutability.js
index 2236f56..3ab90da 100644
--- a/test/mutability.js
+++ b/test/mutability.js
@@ -1,209 +1,226 @@
-var assert = require('assert');
-var Traverse = require('../');
+var test = require('tape');
+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) {
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) {
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) {
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) {
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();
+});
exports.removeNoStop = function() {
var obj = { a : 1, b : 2, c : { d: 3, e: 4 }, f: 5 };
var keys = [];
- Traverse(obj).forEach(function (x) {
+ 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'])
+ t.same(keys, [undefined, 'a', 'b', 'c', 'd', 'e', 'f'])
+ t.end();
}
exports.removeStop = function() {
var obj = { a : 1, b : 2, c : { d: 3, e: 4 }, f: 5 };
var keys = [];
- Traverse(obj).forEach(function (x) {
+ traverse(obj).forEach(function (x) {
keys.push(this.key)
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)
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)
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,
{ a : 1, b : 2, c : [ 3, 4 ] }
));
@@ -211,26 +228,28 @@ exports.deleteMap = function () {
var xs = [ 3, 4 ];
delete xs[1];
- 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,
{ a : 1, b : 2, c : [ 3, 4, 5 ] }
));
@@ -238,15 +257,44 @@ exports.deleteMapRedux = function () {
var xs = [ 3, 4, 5 ];
delete xs[1];
- 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();
+});
+
+test('objectToString', function (t) {
+ var obj = { a : 1, b : 2, c : [ 3, 4 ] };
+ var res = traverse(obj).forEach(function (x) {
+ if (typeof x === 'object' && !this.isRoot) {
+ this.update(JSON.stringify(x));
+ }
+ });
+ t.same(obj, res);
+ t.same(obj, { a : 1, b : 2, c : "[3,4]" });
+ t.end();
+});
+
+test('stringToObject', function (t) {
+ var obj = { a : 1, b : 2, c : "[3,4]" };
+ var res = traverse(obj).forEach(function (x) {
+ if (typeof x === 'string') {
+ this.update(JSON.parse(x));
+ }
+ else if (typeof x === 'number' && x % 2 === 0) {
+ this.update(x * 10);
+ }
+ });
+ t.deepEqual(obj, res);
+ t.deepEqual(obj, { a : 1, b : 20, c : [ 3, 40 ] });
+ t.end();
+});
diff --git a/test/negative.js b/test/negative.js
index f92dfb0..91566c8 100644
--- a/test/negative.js
+++ b/test/negative.js
@@ -1,20 +1,21 @@
-var Traverse = require('../');
-var assert = require('assert');
+var traverse = require('../');
+var test = require('tape');
-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 } ],
'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();
+});
diff --git a/test/obj.js b/test/obj.js
index d46fd38..8bcf58a 100644
--- a/test/obj.js
+++ b/test/obj.js
@@ -1,15 +1,11 @@
-var assert = require('assert');
-var Traverse = require('../');
+var test = require('tape');
+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));
+});
diff --git a/test/siblings.js b/test/siblings.js
index 99c0f1b..c59e557 100644
--- a/test/siblings.js
+++ b/test/siblings.js
@@ -1,7 +1,7 @@
-var assert = require('assert');
+var test = require('tape');
var traverse = require('../');
-exports.siblings = function () {
+test('siblings', function (t) {
var obj = { a : 1, b : 2, c : [ 4, 5, 6 ] };
var res = traverse(obj).reduce(function (acc, x) {
@@ -23,7 +23,7 @@ exports.siblings = function () {
return acc;
}, {});
- assert.deepEqual(res, {
+ t.same(res, {
'/' : { siblings : [], key : undefined, index : -1 },
'/a' : { siblings : [ 'a', 'b', 'c' ], key : 'a', index : 0 },
'/b' : { siblings : [ 'a', 'b', 'c' ], key : 'b', index : 1 },
@@ -32,4 +32,6 @@ exports.siblings = function () {
'/c/1' : { siblings : [ '0', '1', '2' ], key : '1', index : 1 },
'/c/2' : { siblings : [ '0', '1', '2' ], key : '2', index : 2 }
});
-};
+
+ t.end();
+});
diff --git a/test/stop.js b/test/stop.js
index 3529847..9ce15b0 100644
--- a/test/stop.js
+++ b/test/stop.js
@@ -1,7 +1,7 @@
-var assert = require('assert');
+var test = require('tape');
var traverse = require('../');
-exports.stop = function () {
+test('stop', function (t) {
var visits = 0;
traverse('abcdefghij'.split('')).forEach(function (node) {
if (typeof node === 'string') {
@@ -10,10 +10,11 @@ exports.stop = function () {
}
});
- 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) {
if (typeof node === 'string') {
if (node === 'e') this.stop()
@@ -21,10 +22,11 @@ exports.stopMap = function () {
}
}).join('');
- assert.equal(s, 'ABCDEfghij');
-};
+ t.equal(s, 'ABCDEfghij');
+ t.end();
+});
-exports.stopReduce = function () {
+test('stopReduce', function (t) {
var obj = {
a : [ 4, 5 ],
b : [ 6, [ 7, 8, 9 ] ]
@@ -37,5 +39,6 @@ exports.stopReduce = function () {
return acc;
}, []);
- assert.deepEqual(xs, [ 4, 5, 6 ]);
-};
+ t.same(xs, [ 4, 5, 6 ]);
+ t.end();
+});
diff --git a/test/stringify.js b/test/stringify.js
index 932f5d3..f1680d8 100644
--- a/test/stringify.js
+++ b/test/stringify.js
@@ -1,11 +1,11 @@
-var assert = require('assert');
-var Traverse = require('../');
+var test = require('tape');
+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)) {
this.before(function () { s += '[' });
this.post(function (child) {
@@ -31,6 +31,6 @@ exports.stringify = function () {
}
});
- assert.equal(s, JSON.stringify(obj));
-}
-
+ t.equal(s, JSON.stringify(obj));
+ t.end();
+});
diff --git a/test/subexpr.js b/test/subexpr.js
index a217beb..7682608 100644
--- a/test/subexpr.js
+++ b/test/subexpr.js
@@ -1,7 +1,7 @@
var traverse = require('../');
-var assert = require('assert');
+var test = require('tape');
-exports.subexpr = function () {
+test('subexpr', function (t) {
var obj = [ 'a', 4, 'b', 5, 'c', 6 ];
var r = traverse(obj).map(function (x) {
if (typeof x === 'number') {
@@ -9,15 +9,16 @@ exports.subexpr = function () {
}
});
- 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 ],
'b', [ 4.9, 5, 5.1 ],
'c', [ 5.9, 6, 6.1 ],
]);
-};
+ t.end();
+});
-exports.block = function () {
+test('block', function (t) {
var obj = [ [ 1 ], [ 2 ], [ 3 ] ];
var r = traverse(obj).map(function (x) {
if (Array.isArray(x) && !this.isRoot) {
@@ -26,9 +27,10 @@ exports.block = function () {
}
});
- assert.deepEqual(r, [
+ t.same(r, [
[ [ [ [ [ 5 ] ] ] ] ],
[ [ [ [ 5 ] ] ] ],
[ [ [ 5 ] ] ],
]);
-};
+ t.end();
+});
diff --git a/test/super_deep.js b/test/super_deep.js
index acac2fd..1eb9e26 100644
--- a/test/super_deep.js
+++ b/test/super_deep.js
@@ -1,23 +1,24 @@
-var assert = require('assert');
+var test = require('tape');
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();
+});
function make () {
var a = { self : 'a' };
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-traverse.git
More information about the Pkg-javascript-commits
mailing list