Merge pull request #26612 from tdip/master

Change traverse.paths type from string[] to string[][], since the fun…
This commit is contained in:
Nathan Shively-Sanders
2018-06-19 11:08:15 -07:00
committed by GitHub
2 changed files with 27 additions and 2 deletions

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.

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`);
}
})
})
}