Skip to content

Commit

Permalink
Add roots option
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent 79857b4 commit 57eb252
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/config/normalize/lib/wild_wild_path/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { iterate } from './iterate/main.js'
export const list = function (
target,
query,
{ childFirst, sort, missing, classes, inherited } = {},
{ childFirst, roots, sort, missing, classes, inherited } = {},
) {
return [
...iterate(target, query, {
childFirst,
roots,
sort,
missing,
classes,
Expand Down
6 changes: 4 additions & 2 deletions src/config/normalize/lib/wild_wild_path/iterate/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ export const iterate = function* (
query,
{
childFirst = false,
roots = false,
sort = false,
missing = false,
classes = false,
inherited = false,
} = {},
) {
const opts = { childFirst, sort, missing, classes, inherited }
const opts = { childFirst, roots, sort, missing, classes, inherited }
validateInherited(opts)
const parents = new Set([])
const queryArrays = parseQuery(query)
Expand Down Expand Up @@ -58,6 +59,7 @@ const iterateLevel = function* ({
parents.delete(value)
}

// The `roots` option can be used to only include the highest ancestors
// eslint-disable-next-line complexity
const iterateToken = function* ({ entries, index, parents, opts }) {
const entriesA = expandRecursiveTokens(entries, index)
Expand All @@ -68,7 +70,7 @@ const iterateToken = function* ({ entries, index, parents, opts }) {
yield normalizeEntry(parentEntry)
}

if (parentEntry === undefined || entriesB.length !== 1) {
if (parentEntry === undefined || (entriesB.length !== 1 && !opts.roots)) {
yield* iterateChildren({ entries: entriesB, index, parents, opts })
}

Expand Down

0 comments on commit 57eb252

Please sign in to comment.