Skip to content

Commit

Permalink
Use strings for prop tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 27, 2022
1 parent 3c2379a commit df2e6e6
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 28 deletions.
5 changes: 2 additions & 3 deletions src/config/normalize/lib/star_dot_path/entries/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import isPlainObj from 'is-plain-obj'

import { isAnyToken, getPropTokenValue } from '../parsing/node.js'
import { isAnyToken } from '../parsing/node.js'

import { getComplexEntries } from './complex.js'

Expand Down Expand Up @@ -46,8 +46,7 @@ const getAnyEntries = function (value, path) {

// For queries which do not use *, e.g. `a.b` or `a.1`
const getKeyEntries = function (value, path, token) {
const tokenValue = getPropTokenValue(token)
return Array.isArray(value) || isPlainObj(value)
? [{ value: value[tokenValue], path: [...path, tokenValue] }]
? [{ value: value[token], path: [...path, token] }]
: []
}
12 changes: 0 additions & 12 deletions src/config/normalize/lib/star_dot_path/parsing/node.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
// Create a token for property names or array indices
export const createPropToken = function (value) {
return { type: PROP_TYPE, value }
}

// Retrieve the value of a prop token
export const getPropTokenValue = function (token) {
return token.value
}

const PROP_TYPE = 'prop'

// Check if at least one node has some ANY tokens
export const isAnyNodes = function (nodes) {
return nodes.some(isAnyNode)
Expand Down
6 changes: 3 additions & 3 deletions src/config/normalize/lib/star_dot_path/parsing/parse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createAnyToken, createPropToken } from './node.js'
import { createAnyToken } from './node.js'
import { pathToNodes } from './path.js'
import { ESCAPE, SEPARATOR, ANY } from './special.js'

Expand Down Expand Up @@ -59,7 +59,7 @@ export const parse = function (query) {

if (character === SEPARATOR || index === query.length) {
if (chars !== '' || node.length === 0) {
node.push(createPropToken(parseIndex(chars, node)))
node.push(parseIndex(chars, node))
chars = ''
}

Expand All @@ -70,7 +70,7 @@ export const parse = function (query) {

if (character === ANY) {
if (chars !== '') {
node.push(createPropToken(chars))
node.push(chars)
chars = ''
}

Expand Down
6 changes: 3 additions & 3 deletions src/config/normalize/lib/star_dot_path/parsing/path.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isAnyNode, createPropToken, getPropTokenValue } from './node.js'
import { isAnyNode } from './node.js'
import { ANY } from './special.js'

// From an array of property names to an array to nodes
Expand All @@ -7,7 +7,7 @@ export const pathToNodes = function (path) {
}

const getPropNameNode = function (propName) {
return [createPropToken(propName)]
return [propName]
}

// Inverse of `pathToNodes()`
Expand All @@ -20,5 +20,5 @@ const getNodePropName = function (node) {
throw new Error(`Cannot use wildcard "${ANY}" when using nodesToPath().`)
}

return getPropTokenValue(node[0])
return node[0]
}
12 changes: 5 additions & 7 deletions src/config/normalize/lib/star_dot_path/parsing/serialize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isAnyToken, getPropTokenValue } from './node.js'
import { isAnyToken } from './node.js'
import { SEPARATOR, ANY, SPECIAL_CHARS_REGEXP } from './special.js'

// Inverse of `parse()`
Expand All @@ -19,15 +19,13 @@ const serializeToken = function (token, index) {
}

const serializePropToken = function (token, index) {
const value = getPropTokenValue(token)

if (Number.isInteger(value)) {
return String(value)
if (Number.isInteger(token)) {
return String(token)
}

if (value === '' && index === 0) {
if (token === '' && index === 0) {
return SEPARATOR
}

return value.replace(SPECIAL_CHARS_REGEXP, '\\$&')
return token.replace(SPECIAL_CHARS_REGEXP, '\\$&')
}

0 comments on commit df2e6e6

Please sign in to comment.