Permalink
Browse files

fixed merge conflicts

  • Loading branch information...
substack committed Jun 19, 2012
2 parents ef3694f + 28d5fb6 commit 576832a2e4d91197b88a002dec643310fa9b3b26
Showing with 50 additions and 14 deletions.
  1. +23 −14 index.js
  2. +27 −0 test/mutability.js
View
@@ -160,24 +160,31 @@ function walk (root, cb, immutable) {
if (!alive) return state;
- if (typeof node === 'object' && node !== null) {
- state.keys = objectKeys(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,6 +198,8 @@ function walk (root, cb, immutable) {
&& state.node !== null && !state.circular) {
parents.push(state);
+ updateState();
+
forEach(state.keys, function (key, i) {
path.push(key);
View
@@ -271,3 +271,30 @@ test('deleteMapRedux', function (t) {
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();
+});

0 comments on commit 576832a

Please sign in to comment.