Skip to content

Commit

Permalink
Add parsePath() and serializePath()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent 614b157 commit 6171693
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/config/normalize/lib/modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { callValueFunc, callUserFunc, getValidateExampleError } from './call.js'
import { resolvePath } from './path.js'
import { transformValue } from './transform.js'
import { getWarnings } from './warn.js'
import { has, parse } from './wild_wild_path/main.js'
import { has, parsePath } from './wild_wild_path/main.js'

// Once the initial value has been computed, apply validation and transforms,
// unless the value is `undefined`.
Expand Down Expand Up @@ -69,6 +69,6 @@ const renameProp = async function (value, rename, opts) {
return
}

const renamedPath = parse(renameReturn)
const renamedPath = parsePath(renameReturn)
return has(opts.funcOpts.config, renamedPath) ? undefined : renamedPath
}
8 changes: 4 additions & 4 deletions src/config/normalize/lib/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { callUserFunc } from './call.js'
import { getCwd } from './cwd.js'
import { applyMoves } from './move.js'
import { getPrefix, DEFAULT_PREFIX } from './prefix.js'
import { parse, serialize } from './wild_wild_path/main.js'
import { parsePath, serializePath } from './wild_wild_path/main.js'

// Retrieve `opts` passed to most methods.
// `funcOpts` are passed to user-provided functions.
Expand All @@ -18,7 +18,7 @@ export const getOpts = async function ({
moves,
}) {
const originalPath = applyMoves(moves, namePath)
const originalName = serialize(originalPath)
const originalName = serializePath(originalPath)
const funcOpts = {
name: nameQuery,
path: namePath,
Expand All @@ -43,7 +43,7 @@ export const getOpts = async function ({
// `funcOpts.config`.
const computeParent = async function (opts, moves, parent) {
const originalPath = await appendParentToName(parent, opts)
const originalName = serialize(originalPath)
const originalName = serializePath(originalPath)
return { ...opts, funcOpts: { ...opts.funcOpts, originalName, originalPath } }
}

Expand All @@ -53,7 +53,7 @@ const computeParent = async function (opts, moves, parent) {
// By default, there are none.
const appendParentToName = async function (parent, opts) {
const parentA = await callUserFunc(parent, opts)
const parentPath = parse(parentA)
const parentPath = parsePath(parentA)
return [...parentPath, ...opts.funcOpts.originalPath]
}

Expand Down
6 changes: 3 additions & 3 deletions src/config/normalize/lib/rule.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get, parse, serialize } from './wild_wild_path/main.js'
import { get, parseQuery, serializeQuery } from './wild_wild_path/main.js'

// Validate and normalize rules.
// All methods and properties that use queries can use either the string or the
Expand All @@ -8,8 +8,8 @@ export const normalizeRules = function (rules) {
}

const parseName = function ({ name, ...rule }) {
const nameQuery = serialize(name)
const namePath = parse(name)
const nameQuery = serializeQuery(name)
const namePath = parseQuery(name)
return { ...rule, nameQuery, namePath }
}

Expand Down
4 changes: 2 additions & 2 deletions src/config/normalize/lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import isPlainObj from 'is-plain-obj'
import pReduce from 'p-reduce'

import { callValueFunc } from './call.js'
import { parse } from './wild_wild_path/main.js'
import { parsePath } from './wild_wild_path/main.js'

// Apply `transform(value, opts)` which transforms the value set by the user.
// If can also delete it by returning `undefined`.
Expand Down Expand Up @@ -58,7 +58,7 @@ const isTransformMove = function (transformReturn) {
}

const getTransformMove = function ({ value, newProp }) {
const newPropA = parse(newProp)
const newPropA = parsePath(newProp)
return newPropA.length === 0 ? {} : { value, newProp: newPropA }
}

Expand Down
8 changes: 4 additions & 4 deletions src/config/normalize/lib/wild_wild_path/iterate/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { groupBy } from '../../../../../utils/group.js'
import { parse } from '../parsing/parse.js'
import { serialize } from '../parsing/serialize.js'
import { parseQuery } from '../parsing/parse.js'
import { serializePath } from '../parsing/serialize.js'

import { removeDuplicates } from './duplicate.js'
import { expandToken } from './expand.js'
Expand All @@ -11,7 +11,7 @@ import { expandToken } from './expand.js'
// - To allow consumers to return only the first matching entry quickly
// - To keep memory consumption low even on big queries
export const iterate = function* (target, query, { childFirst = false } = {}) {
const queryArrays = parse(query)
const queryArrays = parseQuery(query)
const entries = queryArrays.map((queryArray) => ({
queryArray,
value: target,
Expand Down Expand Up @@ -43,7 +43,7 @@ const getParentEntries = function (entries, index) {
}

const normalizeEntry = function ({ value, path, missing }) {
const query = serialize(path)
const query = serializePath(path)
return { value, path, query, missing }
}

Expand Down
4 changes: 2 additions & 2 deletions src/config/normalize/lib/wild_wild_path/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export {
isSameToken,
isParentPath,
} from './parsing/compare.js'
export { serialize, serializePath } from './parsing/serialize.js'
export { parse, parsePath } from './parsing/parse.js'
export { serializeQuery, serializePath } from './parsing/serialize.js'
export { parseQuery, parsePath } from './parsing/parse.js'
export { remove } from './remove.js'
export { set } from './set.js'
6 changes: 3 additions & 3 deletions src/config/normalize/lib/wild_wild_path/parsing/compare.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { getObjectTokenType } from '../tokens/main.js'

import { validatePath } from './normalize.js'
import { parse } from './parse.js'
import { parseQuery } from './parse.js'

// Check if two queries are equal.
// Works with:
// - Normalization, e.g. `:` === `0:`
// - Unions, e.g. `a b` === `b a`
// - Duplicates, e.g. `a a` === `a`
export const isSameQuery = function (queryA, queryB) {
const queryArraysA = parse(queryA)
const queryArraysB = parse(queryB)
const queryArraysA = parseQuery(queryA)
const queryArraysB = parseQuery(queryB)
return (
queryArraysA.length === queryArraysB.length &&
queryArraysA.every((queryArrayA) =>
Expand Down
6 changes: 4 additions & 2 deletions src/config/normalize/lib/wild_wild_path/parsing/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ 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
// Parse a path
export const parsePath = function (query) {
const queryArrays = parse(query)
const queryArrays = parseQuery(query)
return normalizePath(queryArrays)
}

export const parse = function (query) {
// Parse a query string or array
export const parseQuery = function (query) {
return isQueryString(query)
? safeParseQueryString(query)
: normalizeQueryArrays(query)
Expand Down
8 changes: 4 additions & 4 deletions src/config/normalize/lib/wild_wild_path/parsing/serialize.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { TOKEN_SEPARATOR, ARRAY_SEPARATOR } from '../tokens/escape.js'
import { getObjectTokenType } from '../tokens/main.js'

import { parse, parsePath } from './parse.js'
import { parseQuery, parsePath } from './parse.js'

// Inverse of `parse()`
// Inverse of `parseQuery()`
// When passing a query string, it is parsed and re-serialized to validate and
// normalize it.
export const serialize = function (query) {
const queryArrays = parse(query)
export const serializeQuery = function (query) {
const queryArrays = parseQuery(query)
return queryArrays.map(serializeQueryArray).join(ARRAY_SEPARATOR)
}

Expand Down

0 comments on commit 6171693

Please sign in to comment.