|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
739 bytes
|
| Line | |
|---|
| 1 | /**
|
|---|
| 2 | * Parse a path string into an array of path segments.
|
|---|
| 3 | *
|
|---|
| 4 | * Square bracket notation `a[b]` may be used to "escape" dots that would otherwise be interpreted as path separators.
|
|---|
| 5 | *
|
|---|
| 6 | * Example:
|
|---|
| 7 | * a -> ['a']
|
|---|
| 8 | * a.b.c -> ['a', 'b', 'c']
|
|---|
| 9 | * a[b].c -> ['a', 'b', 'c']
|
|---|
| 10 | * a[b.c].e.f -> ['a', 'b.c', 'e', 'f']
|
|---|
| 11 | * a[b][c][d] -> ['a', 'b', 'c', 'd']
|
|---|
| 12 | *
|
|---|
| 13 | * @param {string|string[]} path
|
|---|
| 14 | **/
|
|---|
| 15 | export function toPath(path) {
|
|---|
| 16 | if (Array.isArray(path)) return path
|
|---|
| 17 |
|
|---|
| 18 | let openBrackets = path.split('[').length - 1
|
|---|
| 19 | let closedBrackets = path.split(']').length - 1
|
|---|
| 20 |
|
|---|
| 21 | if (openBrackets !== closedBrackets) {
|
|---|
| 22 | throw new Error(`Path is invalid. Has unbalanced brackets: ${path}`)
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | return path.split(/\.(?![^\[]*\])|[\[\]]/g).filter(Boolean)
|
|---|
| 26 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.