Change traverse.paths type from string[] to string[][], since the function returns an array of paths.

This commit is contained in:
Ernesto Rodriguez
2018-06-17 04:11:28 -06:00
parent 10fe633823
commit 9f0677bc3d
2 changed files with 27 additions and 2 deletions
+2 -2
View File
@@ -42,7 +42,7 @@ declare namespace traverse {
* Return an `Array` of every possible non-cyclic path in the object.
* Paths are `Array`s of string keys.
*/
function paths(obj: any): string[];
function paths(obj: any): string[][];
/**
* Return an `Array` of every node in the object.
@@ -91,7 +91,7 @@ declare namespace traverse {
* Return an `Array` of every possible non-cyclic path in the object.
* Paths are `Array`s of string keys.
*/
paths(): string[];
paths(): string[][];
/**
* Return an `Array` of every node in the object.
+25
View File
@@ -37,3 +37,28 @@ function testMap(){
});
console.dir(scrubbed);
}
function testPaths(){
let obj = {a: {b: {c: 42}}, d: {e: 44}};
let paths : string[][] = traverse(obj).paths();
const expected = [
[],
['a'],
['a', 'b'],
['a', 'b', 'c'],
['d'],
['d', 'e']
];
expected.forEach((path, ix) => {
const actual = paths[ix];
path.forEach((expectedItem, jx) => {
const actualItem = actual[jx];
if(expectedItem !== actualItem){
throw new Error(`The path ${path} and ${actual} do not macth`);
}
})
})
}