From f8e155e39066c18b863871faa5236dfb48aaf489 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Wed, 15 Dec 2021 09:23:15 -0500 Subject: [PATCH] Cleanup --- src/util/toPath.js | 51 +++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/util/toPath.js b/src/util/toPath.js index 3f1f5f653a9b..ee5ea64d0dfb 100644 --- a/src/util/toPath.js +++ b/src/util/toPath.js @@ -1,3 +1,5 @@ +const IS_COMPLEX_PATH = /\.{2,}|[\[\]]/g + /** * Parse a path string into an array of path segments. * @@ -14,6 +16,13 @@ **/ export function toPath(path) { if (Array.isArray(path)) return path + if (!IS_COMPLEX_PATH.test(path)) return path.split('.') + + return parsePath(path) +} + +function parsePath(path) { + // return parsePathFull(path) let parts = [] let inBrackets = false @@ -100,9 +109,7 @@ export function toPath(path) { } // Herre for reference in case one is preferred over the other -export function toPathFull(path) { - if (Array.isArray(path)) return path - +function parsePathFull(path) { // The general outline/properties of a path string: // Tokens: @@ -136,9 +143,6 @@ export function toPathFull(path) { let error = () => { throw new Error(`Invalid path: ${path}\n` + `${' '.repeat(14 + i)}^`) } - let startNew = () => (partStart = i) - let advance = () => (partEnd = i) - let setInBrackets = (v) => (inBrackets = v) let capture = () => parts.push(path.slice(partStart, partEnd)) if (prev === undefined) { @@ -147,8 +151,8 @@ export function toPathFull(path) { capture() } else if (curr === '[') { // start -> lb - startNew() - setInBrackets(true) + partStart = i + inBrackets = true } else if (curr === ']') { // start -> rb return error() @@ -161,7 +165,7 @@ export function toPathFull(path) { return error() } else { // dot -> ident - startNew() + partStart = i } } else if (prev === '[') { if (curr === '[' || curr === undefined) { @@ -170,18 +174,18 @@ export function toPathFull(path) { } else if (curr === ']') { // lb -> rb capture() - setInBrackets(false) + inBrackets = false } else { // lb -> ident - startNew() + partStart = i } } else if (prev === ']') { if (curr === '.') { // rb -> dot - startNew() + partStart = i } else if (curr === '[') { // rb -> lb - setInBrackets(true) + inBrackets = true } else if (curr === undefined) { // rb -> end } else { @@ -189,30 +193,27 @@ export function toPathFull(path) { return error() } } else { + if (curr === '[' && inBrackets) { + // ident -> lb if in brackets + return error() + } + + partEnd = i + if (curr === '.' && !inBrackets) { // ident -> dot - advance() capture() } else if (curr === ']') { // ident -> rb - advance() capture() - setInBrackets(false) - } else if (curr === '[' && inBrackets) { - // ident -> lb if in brackets - return error() + inBrackets = false } else if (curr === '[' && !inBrackets) { // ident -> lb - advance() capture() - setInBrackets(true) + inBrackets = true } else if (curr === undefined) { // ident -> end - advance() capture() - } else { - // ident -> ident - advance() } } }