add type define for traverse

This commit is contained in:
Ke Peng
2016-02-01 16:25:42 +08:00
parent 6d3c9f422f
commit 3d46732540
2 changed files with 62 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
/// <reference path="traverse.d.ts" />
import traverse = require('traverse');
function testForEach(){
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);
}
function testReduce(){
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);
}
function testMap(){
var c: any[] = [3, 4];
var obj = { a : 1, b : 2, c : c };
obj.c.push(obj);
var scrubbed = traverse(obj).map(function (x) {
if (this.circular) this.remove()
});
console.dir(scrubbed);
}
+22
View File
@@ -0,0 +1,22 @@
// Type definitions for traverse 0.6.6
// Project: https://github.com/substack/js-traverse
// Definitions by: newclear <https://github.com/newclear>
// Definitions: https://github.com/newclear/DefinitelyTyped
declare module "traverse" {
interface Traverse {
get(paths: string[]): any;
has(paths: string[]): boolean;
set(paths: string[], value: any): any;
map(cb: (v: any) => void): any;
forEach(cb: (v: any) => void): any;
reduce(cb: (acc: any, v: any) => void, init?: any): any;
paths(): string[];
nodes(): any[];
clone(): any;
}
function traverse(obj: any): Traverse;
export = traverse;
}