(source: unknown, path: string)
| 11 | * Returns undefined for any missing segment. |
| 12 | */ |
| 13 | export function pluckByPath(source: unknown, path: string): unknown { |
| 14 | if (source === null || source === undefined || !path) return source |
| 15 | const segments = path |
| 16 | .replace(/\[(\w+)\]/g, '.$1') |
| 17 | .split('.') |
| 18 | .filter(Boolean) |
| 19 | let cursor: unknown = source |
| 20 | for (const seg of segments) { |
| 21 | if (cursor === null || cursor === undefined) return undefined |
| 22 | if (typeof cursor !== 'object') return undefined |
| 23 | cursor = (cursor as Record<string, unknown>)[seg] |
| 24 | } |
| 25 | return cursor |
| 26 | } |
no test coverage detected