Skip to content

Commit

Permalink
has() with tests, documented get() and set() too
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Feb 21, 2012
1 parent a23839a commit aeebf14
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 41 deletions.
99 changes: 59 additions & 40 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,65 @@ 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
=======

Expand Down Expand Up @@ -165,46 +224,6 @@ this.post(fn)

Call this function after each of the children are traversed.

methods
=======

.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.

install
=======
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ Traverse.prototype.get = function (ps) {
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 ++) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "traverse",
"version" : "0.5.2",
"version" : "0.6.0",
"description" : "Traverse and transform objects by visiting every node on a recursive walk",
"author" : "James Halliday",
"license" : "MIT/X11",
Expand Down
13 changes: 13 additions & 0 deletions test/has.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var 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)
};

0 comments on commit aeebf14

Please sign in to comment.