Skip to content

Commit

Permalink
Add entries option
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 20, 2022
1 parent 92337e4 commit d07fcff
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
12 changes: 11 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,7 +6,16 @@ import { iterate } from './iterate/main.js'
export const list = function (
target,
query,
{ childFirst, roots, leaves, sort, missing, classes, inherited } = {},
{
childFirst,
roots,
leaves,
sort,
missing,
entries,
classes,
inherited,
} = {},
) {
return [
...iterate(target, query, {
Expand All @@ -15,6 +24,7 @@ export const list = function (
leaves,
sort,
missing,
entries,
classes,
inherited,
}),
Expand Down
23 changes: 14 additions & 9 deletions src/config/normalize/lib/wild_wild_path/iterate/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ const iterateLevel = function* ({
const iterateToken = function* ({ entries, index, parents, opts }) {
const entriesA = expandRecursiveTokens(entries, index)
const entriesB = removeDuplicates(entriesA)
const parentEntry = getParentEntry(entriesB, index)
const parentEntry = getParentEntry(entriesB, index, opts)

if (shouldYieldParentFirst(parentEntry, opts)) {
yield normalizeEntry(parentEntry)
yield parentEntry
}

const hasChildren = yield* iterateChildEntries({
Expand All @@ -72,12 +72,21 @@ const iterateToken = function* ({ entries, index, parents, opts }) {
})

if (shouldYieldParentLast(parentEntry, hasChildren, opts)) {
yield normalizeEntry(parentEntry)
yield parentEntry
}
}

const getParentEntry = function (entries, index) {
return entries.find(({ queryArray }) => queryArray.length === index)
const getParentEntry = function (entries, index, opts) {
const parentEntry = entries.find(
({ queryArray }) => queryArray.length === index,
)
return parentEntry === undefined
? undefined
: normalizeEntry(parentEntry, opts)
}

const normalizeEntry = function ({ value, path, missing }, { entries }) {
return entries ? { value, path, missing } : value
}

const shouldYieldParentFirst = function (parentEntry, { childFirst }) {
Expand All @@ -91,7 +100,3 @@ const shouldYieldParentLast = function (
) {
return parentEntry !== undefined && childFirst && !(leaves && hasChildren)
}

const normalizeEntry = function ({ value, path, missing }) {
return { value, path, missing }
}
9 changes: 8 additions & 1 deletion src/config/normalize/lib/wild_wild_path/iterate/options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { validateInherited, validateLeaves } from '../validate.js'
import {
validateInherited,
validateLeaves,
validateMissing,
} from '../validate.js'

// Add default values and validate options for `iterate()`
export const getOptions = function ({
Expand All @@ -7,6 +11,7 @@ export const getOptions = function ({
leaves = false,
sort = false,
missing = false,
entries = false,
classes = false,
inherited = false,
} = {}) {
Expand All @@ -16,10 +21,12 @@ export const getOptions = function ({
leaves,
sort,
missing,
entries,
classes,
inherited,
}
validateInherited(opts)
validateLeaves(opts)
validateMissing(opts)
return opts
}
11 changes: 11 additions & 0 deletions src/config/normalize/lib/wild_wild_path/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ export const validateInherited = function ({ classes, inherited }) {
}
}

// `missing` entries would be just `undefined` if `entries` is `false`, without
// the consumer knowing their `path` or whether the `undefined` value is missing
// or not.
export const validateMissing = function ({ missing, entries }) {
if (missing && !entries) {
throw new Error(
'The "entries" option must be true when the "missing" option is true.',
)
}
}

// `roots` and `leaves` do opposite things, so are incompatible
export const validateLeaves = function ({ roots, leaves }) {
if (roots && leaves) {
Expand Down

0 comments on commit d07fcff

Please sign in to comment.