Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent c217865 commit 0b82922
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
5 changes: 2 additions & 3 deletions src/config/normalize/lib/wild_wild_path/get.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { iterate, normalizeEntry } from './iterate.js'
import { iterate } from './iterate.js'

// Retrieve all properties in `target` matching a query string.
// Unlike `get|has()` it also return missing entries, letting consumers filter
// them or not.
export const list = function (target, queryOrPath) {
const entries = iterate(target, queryOrPath)
return entries.map(normalizeEntry)
return iterate(target, queryOrPath)
}

// Retrieve a single property's value in `target` matching a query string.
Expand Down
6 changes: 3 additions & 3 deletions src/config/normalize/lib/wild_wild_path/iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const iterate = function (target, queryOrPaths) {
missing: false,
}))
const entriesA = iterateLevel(entries, 0)
return entriesA
const entriesB = entriesA.map(normalizeEntry)
return entriesB
}

const iterateLevel = function (entries, index) {
Expand Down Expand Up @@ -84,8 +85,7 @@ const getLastProp = function ({ props }) {
return props[props.length - 1]
}

// Compute all entries properties from the basic ones
export const normalizeEntry = function ({ value, path, missing }) {
const normalizeEntry = function ({ value, props: path, missing }) {
const query = serialize(path)
return { value, path, query, missing }
}
24 changes: 11 additions & 13 deletions src/config/normalize/lib/wild_wild_path/tokens/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@ const normalize = function ({ type }) {
// Use the token to list entries against a target value.
// We purposely ignore symbol properties by using `Object.keys()`.
const iterate = function (value) {
if (Array.isArray(value)) {
return value.map((childValue, index) => ({
value: childValue,
prop: index,
missing: false,
}))
}

return Object.keys(value).map((childKey) => ({
value: value[childKey],
prop: childKey,
missing: false,
}))
return Array.isArray(value)
? value.map((childValue, index) => ({
value: childValue,
prop: index,
missing: false,
}))
: Object.keys(value).map((childKey) => ({
value: value[childKey],
prop: childKey,
missing: false,
}))
}

// Check if two tokens are the same
Expand Down

0 comments on commit 0b82922

Please sign in to comment.