Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 27, 2022
1 parent c3cbac8 commit 7a02c6a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/config/normalize/lib/star_dot_path/entries.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import isPlainObj from 'is-plain-obj'

import { ANY } from './parse.js'

// List all values (and their associated path) matching a specific query for
// on specific target value.
export const listEntries = function (target, tokens) {
Expand All @@ -11,7 +13,7 @@ const listTokenEntries = function (entries, token) {
}

const getTokenEntries = function ({ value, path }, token) {
return token === '*'
return token === ANY
? getWildcardEntries(value, path)
: getKeyEntries(value, path, token)
}
Expand Down
10 changes: 5 additions & 5 deletions src/config/normalize/lib/star_dot_path/get.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { listEntries } from './entries.js'
import { maybeParse, SEPARATOR } from './parse.js'
import { maybeParse, SEPARATOR, ANY } from './parse.js'

// Retrieve all properties in `target` matching a query string.
// The return value is an object where the key is the path to each value.
Expand Down Expand Up @@ -32,15 +32,15 @@ export const get = function (target, queryOrTokens) {
}

const validateWildcards = function (tokens) {
if (tokens.some(isWildcard)) {
if (tokens.some(isAny)) {
throw new Error(
`Cannot use wildcard "*" when using get(): please use list() instead.`,
`Cannot use wildcard "${ANY}" when using get(): please use list() instead.`,
)
}
}

const isWildcard = function (token) {
return token === '*'
const isAny = function (token) {
return token === ANY
}

// TODO: optimize performance by stopping at soon as one entry is found
Expand Down
1 change: 1 addition & 0 deletions src/config/normalize/lib/star_dot_path/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@ export const isParent = function (parentQuery, childQuery) {
}

const ESCAPE = '\\'
export const ANY = '*'
export const SEPARATOR = '.'
const UNESCAPED_CHARS_REGEXP = /[\\.]/gu

0 comments on commit 7a02c6a

Please sign in to comment.