Skip to content

Commit

Permalink
Split file
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 20, 2022
1 parent 7f6d248 commit 3a3bada
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 52 deletions.
55 changes: 3 additions & 52 deletions src/config/normalize/lib/wild_wild_utils/include/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isParentPath } from '../../wild_wild_parser/main.js'
import { set, remove, list } from '../../wild_wild_path/main.js'
import { set, remove } from '../../wild_wild_path/main.js'

import { reduceParents } from './reduce.js'

// Returns an object with only the properties being queried.
export const pick = function (
Expand Down Expand Up @@ -81,53 +82,3 @@ const excludeEntry = function (
) {
return remove(newTarget, path, { mutate, classes, inherited })
}

// Modify a target object multiple times for each matched property.
const reduceParents = function ({
setFunc,
condition,
target,
newTarget,
query,
sort,
roots,
entries: entriesOpt,
classes,
inherited,
}) {
const entries = list(target, query, {
childFirst: false,
roots,
leaves: false,
sort,
missing: false,
entries: true,
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 }) {
const entryA = entriesOpt ? entry : entry.value
return condition(entryA, target)
}

// This is like the `roots` option. However, we cannot use that option since we
// need to apply `condition()` first.
const hasNoParentSet = function ({ path: pathA }, indexA, entries) {
return entries.every(
(entryB, indexB) => indexA <= indexB || !isParentPath(entryB.path, pathA),
)
}
52 changes: 52 additions & 0 deletions src/config/normalize/lib/wild_wild_utils/include/reduce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { isParentPath } from '../../wild_wild_parser/main.js'
import { list } from '../../wild_wild_path/main.js'

// Modify a target object multiple times for each matched property.
export const reduceParents = function ({
setFunc,
condition,
target,
newTarget,
query,
sort,
roots,
entries: entriesOpt,
classes,
inherited,
}) {
const entries = list(target, query, {
childFirst: false,
roots,
leaves: false,
sort,
missing: false,
entries: true,
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 }) {
const entryA = entriesOpt ? entry : entry.value
return condition(entryA, target)
}

// This is like the `roots` option. However, we cannot use that option since we
// need to apply `condition()` first.
const hasNoParentSet = function ({ path: pathA }, indexA, entries) {
return entries.every(
(entryB, indexB) => indexA <= indexB || !isParentPath(entryB.path, pathA),
)
}

0 comments on commit 3a3bada

Please sign in to comment.