* It takes a string like `foo[x][y][z]` and returns an array like `['foo', 'x', 'y', 'z'] * * @param {string} name - The name of the property to get. * * @returns An array of strings.
(name)
| 23 | * @returns An array of strings. |
| 24 | */ |
| 25 | function parsePropPath(name) { |
| 26 | // foo[x][y][z] -> ['foo', 'x', 'y', 'z'] |
| 27 | // foo.x.y.z -> ['foo', 'x', 'y', 'z'] |
| 28 | // A path is split on `.` and on `[...]` groups. A segment — whether written |
| 29 | // in dot notation or captured inside brackets — may contain any character |
| 30 | // except `.`, `[` and `]`, so a key like `user-name` or `user name` is kept |
| 31 | // literal instead of being split (#5402). `.`, `[` and `]` keep their existing |
| 32 | // meaning, e.g. `foo[bar.baz]` -> ['foo', 'bar', 'baz'] and `[]` is an array push. |
| 33 | // Excluding `[` from the bracket group also makes the match fail fast at the |
| 34 | // next `[`, so a malformed name cannot rescan to the end of the string from |
| 35 | // every unmatched `[` — parsing stays linear in the length of the name. |
| 36 | const path = []; |
| 37 | const pattern = /[^.[\]]+|\[([^.[\]]*)]/g; |
| 38 | let match; |
| 39 | |
| 40 | while ((match = pattern.exec(name)) !== null) { |
| 41 | throwIfDepthExceeded(path.length); |
| 42 | path.push(match[0] === '[]' ? '' : match[1] || match[0]); |
| 43 | } |
| 44 | |
| 45 | return path; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Convert an array to an object. |
no test coverage detected