-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
55 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/config/normalize/lib/wild_wild_utils/include/reduce.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) | ||
} |