Skip to content

Commit

Permalink
traverse now works with all the IEs
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Aug 23, 2011
1 parent 559a6f1 commit 96d9e25
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 79 deletions.
80 changes: 22 additions & 58 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -190,49 +190,6 @@ 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.

.deepEqual(obj)
---------------

Returns a boolean, whether the instance value is equal to the supplied object
along a deep traversal using some opinionated choices.

Some notes:

* RegExps are equal if their .toString()s match, but not functions since
functions can close over different variables.

* Date instances are compared using `.getTime()` just like `assert.deepEqual()`.

* Circular references must refer to the same paths within the data structure for
both objects. For instance, in this snippet:

````javascript
var a = [1];
a.push(a); // a = [ 1, *a ]

var b = [1];
b.push(a); // b = [ 1, [ 1, *a ] ]
````

`a` is not the same as `b` since even though the expansion is the same, the
circular references in each refer to different paths into the data structure.

However, in:

````javascript
var c = [1];
c.push(c); // c = [ 1, *c ];
````

`c` is equal to `a` in a `deepEqual()` because they have the same terminal node
structure.

* Arguments objects are not arrays and neither are they the same as regular
objects.

* Instances created with `new` of String, Boolean, and Number types are never
equal to the native versions.

.paths()
--------

Expand All @@ -249,25 +206,32 @@ Return an `Array` of every node in the object.

Create a deep clone of the object.

installation
============
install
=======

Using [npm](http://npmjs.org) do:

Using npm:
npm install traverse
$ npm install traverse

Or check out the repository and link your development copy:
git clone http://github.com/substack/js-traverse.git
cd js-traverse
npm link .
test
====

You can test traverse with "expresso":http://github.com/visionmedia/expresso
(`npm install expresso`):
js-traverse $ expresso
Using [expresso](http://github.com/visionmedia/expresso) do:

$ expresso

100% wahoo, your stuff is not broken!

hash transforms
===============
in the browser
==============

Use [browserify](https://github.com/substack/node-browserify) to run traverse in
the browser.

traverse has been tested and works with:

This library formerly had a hash transformation component. It has been
[moved to the hashish package](https://github.com/substack/node-hashish).
* Internet Explorer 5.5, 6.0, 7.0, 8.0, 9.0
* Firefox 3.5
* Chrome 6.0
* Opera 10.6
* Safari 5.0
48 changes: 28 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Traverse.prototype.clone = function () {
parents.push(src);
nodes.push(dst);

Object_keys(src).forEach(function (key) {
forEach(Object_keys(src), function (key) {
dst[key] = clone(src[key]);
});

Expand Down Expand Up @@ -175,7 +175,7 @@ function walk (root, cb, immutable) {
&& state.node !== null && !state.circular) {
parents.push(state);

state.keys.forEach(function (key, i) {
forEach(state.keys, function (key, i) {
path.push(key);

if (modifiers.pre) modifiers.pre.call(state, state.node[key], key);
Expand All @@ -201,23 +201,6 @@ function walk (root, cb, immutable) {
})(root).node;
}

var Object_keys = Object.keys || function keys (obj) {
var res = [];
for (var key in obj) res.push(key)
return res;
};

var Array_isArray = Array.isArray || function isArray (xs) {
return Object.prototype.toString.call(xs) === '[object Array]';
};
Object_keys(Traverse.prototype).forEach(function (key) {
Traverse[key] = function (obj) {
var args = [].slice.call(arguments, 1);
var t = Traverse(obj);
return t[key].apply(t, args);
};
});

function copy (src) {
if (typeof src === 'object' && src !== null) {
var dst;
Expand Down Expand Up @@ -248,10 +231,35 @@ function copy (src) {
if (!dst.__proto__) dst.__proto__ = proto;
}

Object_keys(src).forEach(function (key) {
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;
};

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 = Traverse(obj);
return t[key].apply(t, args);
};
});
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.4.6",
"version" : "0.5.0",
"description" : "Traverse and transform objects by visiting every node on a recursive walk",
"author" : "James Halliday",
"license" : "MIT/X11",
Expand Down

0 comments on commit 96d9e25

Please sign in to comment.