Skip to content

Commit

Permalink
Add childFirst
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent dde58af commit 42126db
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/config/normalize/lib/wild_wild_path/iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ import { getObjectTokenType } from './tokens/main.js'

// List all values (and their associated path) matching a specific query for
// on specific target value.
export const iterate = function (target, queryOrPaths) {
export const iterate = function (
target,
queryOrPaths,
{ childFirst = false } = {},
) {
const paths = parse(queryOrPaths)
const entries = paths.map((path) => ({
path,
value: target,
props: [],
missing: false,
}))
const entriesA = iterateLevel(entries, 0)
const entriesA = iterateLevel(entries, childFirst, 0)
const entriesB = entriesA.map(normalizeEntry)
return entriesB
}

const iterateLevel = function (entries, index) {
const iterateLevel = function (entries, childFirst, index) {
const entriesA = removeDuplicates(entries)
const parentEntries = entriesA.filter(({ path }) => path.length === index)

Expand All @@ -39,11 +43,14 @@ const iterateLevel = function (entries, index) {
const nextIndex = index + 1
const childEntries =
levelEntries.length === 1
? iterateLevel(levelEntries, nextIndex)
? iterateLevel(levelEntries, childFirst, nextIndex)
: Object.values(groupBy(levelEntries, getLastProp)).flatMap(
(groupedLevelEntries) => iterateLevel(groupedLevelEntries, nextIndex),
(groupedLevelEntries) =>
iterateLevel(groupedLevelEntries, childFirst, nextIndex),
)
return [...childEntries, ...parentEntries]
return childFirst
? [...childEntries, ...parentEntries]
: [...parentEntries, ...childEntries]
}

const removeDuplicates = function (entries) {
Expand Down

0 comments on commit 42126db

Please sign in to comment.