( path: string, json: unknown, relatedPaths: Set<string> = new Set<string>() )
| 50 | } |
| 51 | |
| 52 | export function getRelatedPathsAtPath( |
| 53 | path: string, |
| 54 | json: unknown, |
| 55 | relatedPaths: Set<string> = new Set<string>() |
| 56 | ): Array<string> { |
| 57 | const initialPath = new JSONHeroPath(path); |
| 58 | const pathDepth = initialPath.components.length; |
| 59 | |
| 60 | for (let index = 0; index < pathDepth; index++) { |
| 61 | const pathToComponent = new JSONHeroPath( |
| 62 | initialPath.components.slice(0, index + 1) |
| 63 | ); |
| 64 | |
| 65 | const value = pathToComponent.first(json); |
| 66 | |
| 67 | if (typeof value === "undefined") { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | //optimisation: we only want to call inferType on non-array types |
| 72 | if (typeof value !== "object" || !Array.isArray(value)) continue; |
| 73 | |
| 74 | const inferredType = inferType(value); |
| 75 | |
| 76 | if (inferredType.name !== "array") continue; |
| 77 | |
| 78 | if (index + 1 === pathDepth) continue; |
| 79 | |
| 80 | for ( |
| 81 | let childIndex = 0; |
| 82 | childIndex < inferredType.value.length; |
| 83 | childIndex++ |
| 84 | ) { |
| 85 | const relatedPath = initialPath.replaceComponent( |
| 86 | index + 1, |
| 87 | `${childIndex}` |
| 88 | ); |
| 89 | |
| 90 | if (relatedPaths.has(relatedPath.toString())) continue; |
| 91 | |
| 92 | const parentValue = relatedPath.parent?.first(json); |
| 93 | |
| 94 | if (!parentValue) continue; |
| 95 | |
| 96 | relatedPaths.add(relatedPath.toString()); |
| 97 | |
| 98 | getRelatedPathsAtPath(relatedPath.toString(), json, relatedPaths); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return Array.from(relatedPaths); |
| 103 | } |
no outgoing calls
no test coverage detected