( obj: JsonValue, path: string[], defaultValue: JsonValue = null, )
| 221 | * @returns The value at the path, or defaultValue if not found |
| 222 | */ |
| 223 | export function getValueAtPath( |
| 224 | obj: JsonValue, |
| 225 | path: string[], |
| 226 | defaultValue: JsonValue = null, |
| 227 | ): JsonValue { |
| 228 | if (path.length === 0) return obj; |
| 229 | |
| 230 | const [first, ...rest] = path; |
| 231 | |
| 232 | if (obj === null || obj === undefined) { |
| 233 | return defaultValue; |
| 234 | } |
| 235 | |
| 236 | if (Array.isArray(obj)) { |
| 237 | const index = Number(first); |
| 238 | if (isNaN(index) || index < 0 || index >= obj.length) { |
| 239 | return defaultValue; |
| 240 | } |
| 241 | return getValueAtPath(obj[index], rest, defaultValue); |
| 242 | } |
| 243 | |
| 244 | if (typeof obj === "object" && obj !== null) { |
| 245 | if (!(first in obj)) { |
| 246 | return defaultValue; |
| 247 | } |
| 248 | return getValueAtPath((obj as JsonObject)[first], rest, defaultValue); |
| 249 | } |
| 250 | |
| 251 | return defaultValue; |
| 252 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…