Skip to content

Commit

Permalink
now with syntax-highlightable markdown snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Jun 3, 2011
1 parent 5eab662 commit d4a7710
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,47 @@ transform negative numbers in-place
-----------------------------------

negative.js
var Traverse = require('traverse');
var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];

Traverse(obj).forEach(function (x) {
if (x < 0) this.update(x + 128);
});

console.dir(obj);

output
````javascript
var traverse = require('traverse');
var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];

traverse(obj).forEach(function (x) {
if (x < 0) this.update(x + 128);
});

console.dir(obj);
````

Output:

[ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ]

collect leaf nodes
------------------

leaves.js
var Traverse = require('traverse');

var obj = {
a : [1,2,3],
b : 4,
c : [5,6],
d : { e : [7,8], f : 9 },
};

var leaves = Traverse(obj).reduce(function (acc, x) {
if (this.isLeaf) acc.push(x);
return acc;
}, []);

console.dir(leaves);

output
````javascript
var traverse = require('traverse');

var obj = {
a : [1,2,3],
b : 4,
c : [5,6],
d : { e : [7,8], f : 9 },
};

var leaves = traverse(obj).reduce(function (acc, x) {
if (this.isLeaf) acc.push(x);
return acc;
}, []);

console.dir(leaves);
````

Output:

[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

context
Expand Down

0 comments on commit d4a7710

Please sign in to comment.