Skip to content

Commit

Permalink
some minor adjustments to expose keys for sibling calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Jul 28, 2011
1 parent 5cb4ecb commit a936bea
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ function walk (root, cb, immutable) {
delete state.parent.node[state.key];
}
},
keys : null,
before : function (f) { modifiers.before = f },
after : function (f) { modifiers.after = f },
pre : function (f) { modifiers.pre = f },
Expand All @@ -235,7 +236,9 @@ function walk (root, cb, immutable) {
if (!alive) return state;

if (typeof node === 'object' && node !== null) {
state.isLeaf = Object.keys(node).length == 0;
state.keys = Object.keys(node);

state.isLeaf = state.keys.length == 0;

for (var i = 0; i < parents.length; i++) {
if (parents[i].node_ === node_) {
Expand All @@ -254,7 +257,7 @@ function walk (root, cb, immutable) {
// use return values to update if defined
var ret = cb.call(state, state.node);
if (ret !== undefined && state.update) state.update(ret);
state.keys = null;

if (modifiers.before) modifiers.before.call(state, state.node);

if (!keepGoing) return state;
Expand All @@ -263,8 +266,7 @@ function walk (root, cb, immutable) {
&& state.node !== null && !state.circular) {
parents.push(state);

var keys = state.keys || Object.keys(state.node);
keys.forEach(function (key, i) {
state.keys.forEach(function (key, i) {
path.push(key);

if (modifiers.pre) modifiers.pre.call(state, state.node[key], key);
Expand All @@ -274,7 +276,7 @@ function walk (root, cb, immutable) {
state.node[key] = child.node;
}

child.isLast = i == keys.length - 1;
child.isLast = i == state.keys.length - 1;
child.isFirst = i == 0;

if (modifiers.post) modifiers.post.call(state, child);
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.4.5",
"version" : "0.4.6",
"description" : "Traverse and transform objects by visiting every node on a recursive walk",
"author" : "James Halliday",
"license" : "MIT/X11",
Expand Down
1 change: 0 additions & 1 deletion test/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ exports['sort test'] = function () {
'Traversal in a custom order'
);
};

35 changes: 35 additions & 0 deletions test/siblings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var assert = require('assert');
var traverse = require('traverse');

exports.siblings = function () {
var obj = { a : 1, b : 2, c : [ 4, 5, 6 ] };

var res = traverse(obj).reduce(function (acc, x) {
var p = '/' + this.path.join('/');
if (this.parent) {
acc[p] = {
siblings : this.parent.keys,
key : this.key,
index : this.parent.keys.indexOf(this.key)
};
}
else {
acc[p] = {
siblings : [],
key : this.key,
index : -1
}
}
return acc;
}, {});

assert.deepEqual(res, {
'/' : { siblings : [], key : undefined, index : -1 },
'/a' : { siblings : [ 'a', 'b', 'c' ], key : 'a', index : 0 },
'/b' : { siblings : [ 'a', 'b', 'c' ], key : 'b', index : 1 },
'/c' : { siblings : [ 'a', 'b', 'c' ], key : 'c', index : 2 },
'/c/0' : { siblings : [ '0', '1', '2' ], key : '0', index : 0 },
'/c/1' : { siblings : [ '0', '1', '2' ], key : '1', index : 1 },
'/c/2' : { siblings : [ '0', '1', '2' ], key : '2', index : 2 }
});
};

0 comments on commit a936bea

Please sign in to comment.