Skip to content

Commit

Permalink
Add mutate to map() and exclude()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent cea64bd commit 080d690
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
31 changes: 20 additions & 11 deletions src/config/normalize/lib/wild_wild_path_utils/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,32 @@ export const include = function (
}

const pickEntry = function (classes, target, { path, value }) {
return set(target, path, value, { classes })
return set(target, path, value, { classes, mutate: false })
}

// Remove values matching a query
// eslint-disable-next-line max-params
export const exclude = function (target, query, condition, { classes } = {}) {
return reduceParents(excludeEntry.bind(undefined, classes), condition, {
target,
newTarget: target,
query,
sort: false,
classes,
})
export const exclude = function (
target,
query,
condition,
{ classes, mutate } = {},
) {
return reduceParents(
excludeEntry.bind(undefined, { classes, mutate }),
condition,
{
target,
newTarget: target,
query,
sort: false,
classes,
},
)
}

const excludeEntry = function (classes, target, { path }) {
return remove(target, path, { classes })
const excludeEntry = function ({ classes, mutate }, target, { path }) {
return remove(target, path, { classes, mutate })
}

// Modify a target object multiple times for each matched property.
Expand Down
8 changes: 5 additions & 3 deletions src/config/normalize/lib/wild_wild_path_utils/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import { list, get, set } from '../wild_wild_path/main.js'
// - When needed, this can also be done by the consumer logic
// - This also avoids infinite recursion
// eslint-disable-next-line max-params
export const map = function (target, query, mapFunc, { classes } = {}) {
export const map = function (target, query, mapFunc, { classes, mutate } = {}) {
const entries = list(target, query, {
childFirst: true,
sort: false,
classes,
})
return entries.reduce(
(targetA, entry) => mapEntry({ mapFunc, target: targetA, entry, classes }),
(targetA, entry) =>
mapEntry({ mapFunc, target: targetA, entry, classes, mutate }),
target,
)
}
Expand All @@ -30,10 +31,11 @@ const mapEntry = function ({
target,
entry: { path, query, missing },
classes,
mutate,
}) {
const value = get(target, path, { classes })
const mappedValue = mapFunc({ path, query, value, missing })
return value === mappedValue
? target
: set(target, path, mappedValue, { classes })
: set(target, path, mappedValue, { classes, mutate })
}

0 comments on commit 080d690

Please sign in to comment.