Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit b166e96

Browse files
committed
traverse: patch traverse so that we can keep track of when errors are internal
This means that we are using our own copy of traverse again.
1 parent e749675 commit b166e96

32 files changed

Lines changed: 1951 additions & 1 deletion
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- 0.6
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Copyright 2010 James Halliday (mail@substack.net)
2+
3+
This project is free software released under the MIT/X11 license:
4+
http://www.opensource.org/licenses/mit-license.php
5+
6+
Copyright 2010 James Halliday (mail@substack.net)
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var traverse = require('traverse');
2+
3+
var id = 54;
4+
var callbacks = {};
5+
var obj = { moo : function () {}, foo : [2,3,4, function () {}] };
6+
7+
var scrubbed = traverse(obj).map(function (x) {
8+
if (typeof x === 'function') {
9+
callbacks[id] = { id : id, f : x, path : this.path };
10+
this.update('[Function]');
11+
id++;
12+
}
13+
});
14+
15+
console.dir(scrubbed);
16+
console.dir(callbacks);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var traverse = require('traverse');
2+
3+
var obj = {
4+
a : [1,2,3],
5+
b : 4,
6+
c : [5,6],
7+
d : { e : [7,8], f : 9 },
8+
};
9+
10+
var leaves = traverse(obj).reduce(function (acc, x) {
11+
if (this.isLeaf) acc.push(x);
12+
return acc;
13+
}, []);
14+
15+
console.dir(leaves);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var traverse = require('traverse');
2+
var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];
3+
4+
traverse(obj).forEach(function (x) {
5+
if (x < 0) this.update(x + 128);
6+
});
7+
8+
console.dir(obj);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// scrub out circular references
2+
var traverse = require('traverse');
3+
4+
var obj = { a : 1, b : 2, c : [ 3, 4 ] };
5+
obj.c.push(obj);
6+
7+
var scrubbed = traverse(obj).map(function (x) {
8+
if (this.circular) this.remove()
9+
});
10+
console.dir(scrubbed);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env node
2+
var traverse = require('traverse');
3+
4+
var obj = [ 'five', 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];
5+
6+
var s = '';
7+
traverse(obj).forEach(function to_s (node) {
8+
if (Array.isArray(node)) {
9+
this.before(function () { s += '[' });
10+
this.post(function (child) {
11+
if (!child.isLast) s += ',';
12+
});
13+
this.after(function () { s += ']' });
14+
}
15+
else if (typeof node == 'object') {
16+
this.before(function () { s += '{' });
17+
this.pre(function (x, key) {
18+
to_s(key);
19+
s += ':';
20+
});
21+
this.post(function (child) {
22+
if (!child.isLast) s += ',';
23+
});
24+
this.after(function () { s += '}' });
25+
}
26+
else if (typeof node == 'string') {
27+
s += '"' + node.toString().replace(/"/g, '\\"') + '"';
28+
}
29+
else if (typeof node == 'function') {
30+
s += 'null';
31+
}
32+
else {
33+
s += node.toString();
34+
}
35+
});
36+
37+
console.log('JSON.stringify: ' + JSON.stringify(obj));
38+
console.log('this stringify: ' + s);

0 commit comments

Comments
 (0)