Skip to content

Commit

Permalink
Improve include()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent bcd6ef0 commit 66bf00e
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/config/normalize/lib/wild_wild_path_utils/pick.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { set } from '../wild_wild_path/main.js'

import { listExisting, reduceParents } from './common.js'
import { reduceParents } from './common.js'

// Returns an object with only the properties being queried.
export const pick = function (target, queryOrPaths) {
Expand All @@ -17,17 +17,12 @@ const pickEntry = function (target, { path, value }) {
}

// Remove values matching a query
export const include = function (target, queryOrPath, condition) {
const entries = listExisting(target, queryOrPath)
return entries.reduce(includeEntry.bind(undefined, condition), {})
}

const includeEntry = function (
condition,
target,
{ path, query, value, missing },
) {
return condition({ path, query, value, missing })
? set(target, path, value)
: target
export const include = function (target, queryOrPaths, condition) {
return reduceParents({
target,
newTarget: {},
queryOrPaths,
setFunc: pickEntry,
condition,
})
}

0 comments on commit 66bf00e

Please sign in to comment.