Skip to content

Commit

Permalink
include circular ref example in the readme, Traverse => traverse
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Jul 20, 2011
1 parent ec1fb18 commit 4a6285f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
21 changes: 21 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ Output:

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

scrub circular references
-------------------------

scrub.js:

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

var obj = { a : 1, b : 2, c : [ 3, 4 ] };
obj.c.push(obj);

var scrubbed = traverse(obj).map(function (x) {
if (this.circular) this.remove()
});
console.dir(scrubbed);
````

output:

{ a: 1, b: 2, c: [ 3, 4 ] }

context
=======

Expand Down
4 changes: 2 additions & 2 deletions examples/json.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var Traverse = require('traverse');
var traverse = require('traverse');

var id = 54;
var callbacks = {};
var obj = { moo : function () {}, foo : [2,3,4, function () {}] };

var scrubbed = Traverse(obj).map(function (x) {
var scrubbed = traverse(obj).map(function (x) {
if (typeof x === 'function') {
callbacks[id] = { id : id, f : x, path : this.path };
this.update('[Function]');
Expand Down
4 changes: 2 additions & 2 deletions examples/leaves.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Traverse = require('traverse');
var traverse = require('traverse');

var obj = {
a : [1,2,3],
Expand All @@ -7,7 +7,7 @@ var obj = {
d : { e : [7,8], f : 9 },
};

var leaves = Traverse(obj).reduce(function (acc, x) {
var leaves = traverse(obj).reduce(function (acc, x) {
if (this.isLeaf) acc.push(x);
return acc;
}, []);
Expand Down
4 changes: 2 additions & 2 deletions examples/negative.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var Traverse = require('traverse');
var traverse = require('traverse');
var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];

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

Expand Down
4 changes: 2 additions & 2 deletions examples/stringify.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node
var Traverse = require('traverse');
var traverse = require('traverse');

var obj = [ 'five', 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];

var s = '';
Traverse(obj).forEach(function to_s (node) {
traverse(obj).forEach(function to_s (node) {
if (Array.isArray(node)) {
this.before(function () { s += '[' });
this.post(function (child) {
Expand Down

0 comments on commit 4a6285f

Please sign in to comment.