Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 20, 2022
1 parent 0e4d87d commit 300ba58
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions src/config/normalize/lib/wild_wild_utils/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ export const pick = function (
{ sort, classes, inherited } = {},
) {
const setFunc = pickEntry.bind(undefined, { classes, inherited })
const entries = listEntries(target, query, {
return reduceParents({
setFunc,
target,
newTarget: {},
query,
roots: true,
sort,
classes,
inherited,
})
return entries.reduce(setFunc, {})
}

// Remove values not matching a query
Expand All @@ -26,10 +29,13 @@ export const include = function (
{ sort, entries, classes, inherited } = {},
) {
const setFunc = pickEntry.bind(undefined, { classes, inherited })
return reduceParents(setFunc, condition, {
return reduceParents({
setFunc,
condition,
target,
newTarget: {},
query,
roots: false,
sort,
entries,
classes,
Expand All @@ -54,10 +60,13 @@ export const exclude = function (
{ mutate, entries, classes, inherited } = {},
) {
const setFunc = excludeEntry.bind(undefined, { mutate, classes, inherited })
return reduceParents(setFunc, condition, {
return reduceParents({
setFunc,
condition,
target,
newTarget: target,
query,
roots: false,
sort: false,
entries,
classes,
Expand All @@ -74,29 +83,19 @@ const excludeEntry = function (
}

// Modify a target object multiple times for each matched property.
const reduceParents = function (
const reduceParents = function ({
setFunc,
condition,
{ target, newTarget, query, sort, entries: entriesOpt, classes, inherited },
) {
const entries = listEntries(target, query, {
roots: false,
sort,
classes,
inherited,
})
return entries
.filter((entry) => meetsCondition({ condition, entry, target, entriesOpt }))
.filter(hasNoParentSet)
.reduce(setFunc, newTarget)
}

const listEntries = function (
target,
newTarget,
query,
{ roots, sort, classes, inherited },
) {
return list(target, query, {
sort,
roots,
entries: entriesOpt,
classes,
inherited,
}) {
const entries = list(target, query, {
childFirst: false,
roots,
leaves: false,
Expand All @@ -106,6 +105,18 @@ const listEntries = function (
classes,
inherited,
})
const entriesA = filterEntries({ entries, condition, target, entriesOpt })
return entriesA.reduce(setFunc, newTarget)
}

const filterEntries = function ({ entries, condition, target, entriesOpt }) {
return condition === undefined
? entries
: entries
.filter((entry) =>
meetsCondition({ condition, entry, target, entriesOpt }),
)
.filter(hasNoParentSet)
}

const meetsCondition = function ({ condition, entry, target, entriesOpt }) {
Expand Down

0 comments on commit 300ba58

Please sign in to comment.