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 3d0a97d commit 1456c26
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/config/normalize/lib/wild_wild_path/iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,13 @@ export const iterate = function* (

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

if (!childFirst) {
yield* parentEntries
}

if (parentEntries.length === entriesA.length) {
if (childFirst) {
yield* parentEntries
}

return
}

yield* iterateChildEntries(entries, childFirst, index)
yield* iterateChildEntries(entries, parentEntries, childFirst, index)

if (childFirst) {
yield* parentEntries
Expand Down Expand Up @@ -74,6 +64,15 @@ const isDuplicate = function (
)
}

const getParentEntries = function (entries, index) {
return entries.filter(({ path }) => path.length === index).map(normalizeEntry)
}

const normalizeEntry = function ({ value, simplePath: path, missing }) {
const query = serialize(path)
return { value, path, query, missing }
}

// Iteration among siglings is not sorted, for performance reasons.
// - Consumers can sort it through using the `query` property
// However, iteration is guaranteed to return child entries before parent ones.
Expand Down Expand Up @@ -111,7 +110,17 @@ export const handleMissingValue = function (value, token) {
return { tokenType, missing, value: valueA }
}

const iterateChildEntries = function* (entries, childFirst, index) {
// eslint-disable-next-line max-params
const iterateChildEntries = function* (
entries,
parentEntries,
childFirst,
index,
) {
if (parentEntries.length === entries.length) {
return
}

const levelEntries = entries
.filter(({ path }) => path.length !== index)
.flatMap((entry) => iteratePath(entry, index))
Expand Down Expand Up @@ -141,8 +150,3 @@ const iterateChildren = function* (levelEntries, childFirst, index) {
const getLastProp = function ({ simplePath }) {
return simplePath[simplePath.length - 1]
}

const normalizeEntry = function ({ value, simplePath: path, missing }) {
const query = serialize(path)
return { value, path, query, missing }
}

0 comments on commit 1456c26

Please sign in to comment.