From 96d9e2564fc0f1413a7a1371cfd9cc5600896771 Mon Sep 17 00:00:00 2001 From: James Halliday Date: Tue, 23 Aug 2011 05:51:02 -0700 Subject: [PATCH] traverse now works with all the IEs --- README.markdown | 80 ++++++++++++++----------------------------------- index.js | 48 ++++++++++++++++------------- package.json | 2 +- 3 files changed, 51 insertions(+), 79 deletions(-) diff --git a/README.markdown b/README.markdown index 0734006..053dc2d 100644 --- a/README.markdown +++ b/README.markdown @@ -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() -------- @@ -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 diff --git a/index.js b/index.js index 1dd8b39..980c56f 100644 --- a/index.js +++ b/index.js @@ -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]); }); @@ -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); @@ -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; @@ -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); + }; +}); diff --git a/package.json b/package.json index 69618e4..248e83f 100644 --- a/package.json +++ b/package.json @@ -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",