-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
170 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import isPlainObj from 'is-plain-obj' | ||
|
||
import { isAnyPart } from '../parsing/special.js' | ||
|
||
import { getComplexEntries } from './complex.js' | ||
|
||
// List all values (and their associated path) matching a specific query for | ||
// on specific target value. | ||
export const listEntries = function (target, tokens) { | ||
return tokens.reduce(listTokenEntries, [{ value: target, path: [] }]) | ||
} | ||
|
||
const listTokenEntries = function (entries, token) { | ||
return entries.flatMap((entry) => getTokenEntries(entry, token)) | ||
} | ||
|
||
const getTokenEntries = function ({ value, path }, token) { | ||
if (token.length > 1) { | ||
return getComplexEntries(value, path, token) | ||
} | ||
|
||
const [part] = token | ||
return isAnyPart(part) | ||
? getAnyEntries(value, path) | ||
: getKeyEntries(value, path, part) | ||
} | ||
|
||
// For queries which use * on its own, e.g. `a.*` | ||
const getAnyEntries = function (value, path) { | ||
if (Array.isArray(value)) { | ||
return value.map((childValue, index) => ({ | ||
value: childValue, | ||
path: [...path, index], | ||
})) | ||
} | ||
|
||
if (isPlainObj(value)) { | ||
return Object.entries(value).map(([childKey, childValue]) => ({ | ||
value: childValue, | ||
path: [...path, childKey], | ||
})) | ||
} | ||
|
||
return [] | ||
} | ||
|
||
// For queries which do not use *, e.g. `a.b` or `a.1` | ||
const getKeyEntries = function (value, path, part) { | ||
return Array.isArray(value) || isPlainObj(value) | ||
? [{ value: value[part], path: [...path, part] }] | ||
: [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export { set, remove } from './set.js' | ||
export { list, get, has } from './get.js' | ||
export { pathToTokens, tokensToPath } from './parsing/path.js' | ||
export { isParent } from './parsing/parent.js' | ||
export { serialize } from './parsing/serialize.js' | ||
export { parse } from './parsing/parse.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { SEPARATOR } from './special.js' | ||
|
||
// Check if a query is a parent of another | ||
export const isParent = function (parentQuery, childQuery) { | ||
return childQuery.startsWith(`${parentQuery}${SEPARATOR}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ANY, isAnyToken } from './special.js' | ||
|
||
// From an array of property names to an array to tokens | ||
export const pathToTokens = function (path) { | ||
return path.map(getPropNameToken) | ||
} | ||
|
||
const getPropNameToken = function (propName) { | ||
return [propName] | ||
} | ||
|
||
// Inverse of `pathToTokens()` | ||
export const tokensToPath = function (tokens) { | ||
return tokens.map(getTokenPropName) | ||
} | ||
|
||
const getTokenPropName = function (token) { | ||
if (isAnyToken(token)) { | ||
throw new Error(`Cannot use wildcard "${ANY}" when using tokensToPath().`) | ||
} | ||
|
||
return token[0] | ||
} |
26 changes: 26 additions & 0 deletions
26
src/config/normalize/lib/star_dot_path/parsing/serialize.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { SEPARATOR, ANY, SPECIAL_CHARS_REGEXP, isAnyPart } from './special.js' | ||
|
||
// Inverse of `parse()` | ||
export const serialize = function (tokens) { | ||
return tokens.map(serializeToken).join(SEPARATOR) | ||
} | ||
|
||
const serializeToken = function (token, index) { | ||
if (index === 0 && token[0] === '') { | ||
return SEPARATOR | ||
} | ||
|
||
return token.map(serializePart).join('') | ||
} | ||
|
||
const serializePart = function (part) { | ||
if (Number.isInteger(part)) { | ||
return String(part) | ||
} | ||
|
||
if (isAnyPart(part)) { | ||
return ANY | ||
} | ||
|
||
return part.replace(SPECIAL_CHARS_REGEXP, '\\$&') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import isPlainObj from 'is-plain-obj' | ||
|
||
// Check if at least one token has some ANY parts | ||
export const isAnyTokens = function (tokens) { | ||
return tokens.some(isAnyToken) | ||
} | ||
|
||
// Check if a token has some ANY parts | ||
export const isAnyToken = function (token) { | ||
return token.some(isAnyPart) | ||
} | ||
|
||
// Check if a token part is ANY | ||
export const isAnyPart = function (part) { | ||
return isPlainObj(part) && part.type === ANY_TYPE | ||
} | ||
|
||
export const createAnyPart = function () { | ||
return { type: ANY_TYPE } | ||
} | ||
|
||
const ANY_TYPE = 'any' | ||
|
||
// All special characters | ||
export const ESCAPE = '\\' | ||
export const SEPARATOR = '.' | ||
export const ANY = '*' | ||
// Matches any special characters | ||
export const SPECIAL_CHARS_REGEXP = /[\\.*]/gu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.