mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-31 19:12:20 +08:00
Merge pull request #26612 from tdip/master
Change traverse.paths type from string[] to string[][], since the fun…
This commit is contained in:
4
types/traverse/index.d.ts
vendored
4
types/traverse/index.d.ts
vendored
@@ -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.
|
||||
|
||||
@@ -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`);
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user