(path: string)
| 16 | * @param {string} path |
| 17 | **/ |
| 18 | export function toKeyPath(path: string) { |
| 19 | let keypath: string[] = [] |
| 20 | |
| 21 | for (let part of segment(path, '.')) { |
| 22 | if (!part.includes('[')) { |
| 23 | keypath.push(part) |
| 24 | continue |
| 25 | } |
| 26 | |
| 27 | let currentIndex = 0 |
| 28 | |
| 29 | while (true) { |
| 30 | let bracketL = part.indexOf('[', currentIndex) |
| 31 | let bracketR = part.indexOf(']', bracketL) |
| 32 | |
| 33 | if (bracketL === -1 || bracketR === -1) { |
| 34 | break |
| 35 | } |
| 36 | |
| 37 | // Add the part before the bracket as a key |
| 38 | if (bracketL > currentIndex) { |
| 39 | keypath.push(part.slice(currentIndex, bracketL)) |
| 40 | } |
| 41 | |
| 42 | // Add the part inside the bracket as a key |
| 43 | keypath.push(part.slice(bracketL + 1, bracketR)) |
| 44 | currentIndex = bracketR + 1 |
| 45 | } |
| 46 | |
| 47 | // Add the part after the last bracket as a key |
| 48 | if (currentIndex <= part.length - 1) { |
| 49 | keypath.push(part.slice(currentIndex)) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return keypath |
| 54 | } |
no test coverage detected