Skip to content
Browse files

node.update() takes a string instead of a function now

  • Loading branch information...
1 parent c90401d commit 1ea5f35023afbba15ce4e50fdbf95b0fd0f25d9e @substack committed Apr 26, 2012
Showing with 6 additions and 9 deletions.
  1. +1 −3 example/array.js
  2. +5 −6 index.js
View
4 example/array.js
@@ -8,9 +8,7 @@ var src = '(' + function () {
var output = falafel(src, function (node) {
if (node.type === 'ArrayExpression') {
- node.update(function (s) {
- return 'fn(' + s + ')';
- });
+ node.update('fn(' + node.source() + ')');
}
});
console.log(output);
View
11 index.js
@@ -7,23 +7,22 @@ module.exports = function (src, fn) {
var output = src.split('');
var index = 0;
- function insertUpdate (node) {
+ function insertHelpers (node) {
if (!node.range) return;
node.source = function () {
return output.slice(node.range[0], node.range[1] + 1).join('');
};
- node.update = function (cb) {
- var res = cb(node.source());
- output[node.range[0]] = res;
+ node.update = function (s) {
+ output[node.range[0]] = s;
for (var i = node.range[0] + 1; i < node.range[1] + 1; i++) {
output[i] = '';
}
};
}
(function walk (node) {
- insertUpdate(node);
+ insertHelpers(node);
Object.keys(node).forEach(function (key) {
var child = node[key];
@@ -35,7 +34,7 @@ module.exports = function (src, fn) {
});
}
else if (child && typeof child === 'object' && child.type) {
- insertUpdate(child);
+ insertHelpers(child);
walk(child);
}
});

0 comments on commit 1ea5f35

Please sign in to comment.
Something went wrong with that request. Please try again.