Skip to content

Commit

Permalink
Add parsePath()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent 96a154e commit fb5c03b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/config/normalize/lib/wild_wild_path/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export {
isParentPath,
} from './parsing/compare.js'
export { serialize } from './parsing/serialize.js'
export { parse } from './parsing/parse.js'
export { parse, parsePath } from './parsing/parse.js'
export { remove } from './remove.js'
export { set } from './set.js'
11 changes: 11 additions & 0 deletions src/config/normalize/lib/wild_wild_path/parsing/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ export const isQueryString = function (query) {
return typeof query === 'string'
}

// Transform a queryArrays into a path, if possible
export const normalizePath = function (queryArrays) {
if (queryArrays.length !== 1) {
throwQueryArraysError(queryArrays, 'It must not be a union.')
}

const [queryArray] = queryArrays
validatePath(queryArray)
return queryArray
}

// Paths are a subset of query arrays which use:
// - No unions
// - Only prop tokens, and array tokens (positive only)
Expand Down
11 changes: 10 additions & 1 deletion src/config/normalize/lib/wild_wild_path/parsing/parse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { normalizeQueryArrays, isQueryString } from './normalize.js'
import {
normalizeQueryArrays,
isQueryString,
normalizePath,
} from './normalize.js'
import { parseQueryString } from './query.js'

// Parse a query string into an array of tokens.
Expand Down Expand Up @@ -77,6 +81,11 @@ import { parseQueryString } from './query.js'
// Exceptions are thrown on syntax errors:
// - I.e. query or path syntax errors, or wrong arguments
// - But queries matching nothing do not throw: instead they return nothing
export const parsePath = function (query) {
const queryArrays = parse(query)
return normalizePath(queryArrays)
}

export const parse = function (query) {
return isQueryString(query)
? safeParseQueryString(query)
Expand Down

0 comments on commit fb5c03b

Please sign in to comment.