Skip to content

Commit

Permalink
modifiers seem to work
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Sep 4, 2010
1 parent e7ec7de commit f0ee567
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions lib/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function Traverse (refObj) {
node : this.value,
root : this.value,
callback : cb
})
});
return this;

};
Expand Down Expand Up @@ -107,9 +107,7 @@ function walk (params) {
var parents = params.parents || [];
var cb = params.callback;

var before = null;
var after = null;
var between = null;
var modifiers = {};

var state = {
node : node,
Expand All @@ -128,12 +126,10 @@ function walk (params) {
}
state.node = x;
},
before : function (f) { before = f },
after : function (f) { after = f },
between : function (f, acc) {
between = f;
between._acc = acc;
}
before : function (f) { modifiers.before = f },
after : function (f) { modifiers.after = f },
pre : function (f) { modifiers.pre = f },
post : function (f) { modifiers.post = f }
};

if (typeof node == 'object' && node !== null) {
Expand All @@ -150,38 +146,39 @@ function walk (params) {
state.notLeaf = !state.isLeaf;
state.notRoot = !state.isRoot;

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

// use return values to update if defined
var ret = cb.call(state, node);
if (ret !== undefined && state.update) state.update(ret);
if (modifiers.before) modifiers.before.call(state, state.node);

if (typeof state.node == 'object'
&& state.node !== null && !state.circular) {
parents.push(state);
Object.keys(state.node).forEach(function (key, i) {

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

if (modifiers.pre) modifiers.pre.call(state, state.node[key], key);

var child = walk({
node : state.node[key],
root : root,
path : path,
parents : parents,
callback : cb
});
child.isLast = i == keys.length - 1;
child.isFirst = i == 0;

if (i == 0 && between && between._acc === undefined) {
between._acc = state.node[key]
}
else if (between) {
between._acc = between.call(
state, between._acc, state.node[key]
);
}
if (modifiers.post) modifiers.post.call(state, child);

path.pop();
});
parents.pop();
}

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

return state;
}

0 comments on commit f0ee567

Please sign in to comment.