Skip to content

Commit

Permalink
upgraded readme
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Dec 17, 2012
1 parent c889666 commit c45db75
Showing 1 changed file with 34 additions and 85 deletions.
119 changes: 34 additions & 85 deletions readme.markdown
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
traverse
========
# traverse

Traverse and transform objects by visiting every node on a recursive walk.

[![build status](https://secure.travis-ci.org/substack/js-traverse.png)](http://travis-ci.org/substack/js-traverse)

examples
========
# examples

transform negative numbers in-place
-----------------------------------
## transform negative numbers in-place

negative.js

Expand All @@ -28,8 +25,7 @@ Output:

[ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ]

collect leaf nodes
------------------
## collect leaf nodes

leaves.js

Expand All @@ -55,8 +51,7 @@ Output:

[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

scrub circular references
-------------------------
## scrub circular references

scrub.js:

Expand All @@ -76,26 +71,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))
Expand All @@ -104,155 +95,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
====
# license

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
MIT

0 comments on commit c45db75

Please sign in to comment.