Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent af9eebd commit e9edefa
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 97 deletions.
44 changes: 0 additions & 44 deletions src/config/normalize/lib/wild_wild_path_utils/common.js

This file was deleted.

23 changes: 0 additions & 23 deletions src/config/normalize/lib/wild_wild_path_utils/exclude.js

This file was deleted.

89 changes: 89 additions & 0 deletions src/config/normalize/lib/wild_wild_path_utils/include.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { set, get, remove, iterate, parent } from '../wild_wild_path/main.js'

// Returns an object with only the properties being queried.
export const pick = function (target, queryOrPaths) {
return reduceParents({
target,
newTarget: {},
queryOrPaths,
setFunc: pickEntry,
})
}

const pickEntry = function (target, { path, value }) {
return set(target, path, value)
}

// Remove values matching a query
export const include = function (target, queryOrPaths, condition) {
return reduceParents({
target,
newTarget: {},
queryOrPaths,
setFunc: pickEntry,
condition,
})
}

// Remove values matching a query
export const exclude = function (target, queryOrPaths, condition) {
return reduceParents({
target,
newTarget: target,
queryOrPaths,
setFunc: excludeEntry,
condition: shouldExclude.bind(undefined, condition),
})
}

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

const shouldExclude = function (condition, { path, query, missing }, target) {
const value = get(target, path)
return condition({ path, query, value, missing })
}

// Modify a target object multiple times for each matched property.
// Ignore properties when one of their ancestors was matched too.
// Uses `iterate()` to keep memory consumption low.
const reduceParents = function ({
target,
newTarget,
queryOrPaths,
setFunc,
condition = returnTrue,
}) {
const paths = []

// eslint-disable-next-line fp/no-loops
for (const entry of iterate(target, queryOrPaths)) {
// eslint-disable-next-line max-depth
if (shouldUseEntry({ entry, paths, newTarget, condition })) {
// eslint-disable-next-line fp/no-mutating-methods
paths.push(entry.path)
// eslint-disable-next-line fp/no-mutation, no-param-reassign
newTarget = setFunc(newTarget, entry, 0)
}
}

return newTarget
}

const returnTrue = function () {
return true
}

const shouldUseEntry = function ({ entry, paths, newTarget, condition }) {
return (
!entry.missing &&
!parentIsSet(paths, entry.path) &&
condition(entry, newTarget)
)
}

// If both a parent and a child property are set, the parent prevails
const parentIsSet = function (paths, path) {
return paths.some((previousPath) => parent(previousPath, path))
}
3 changes: 1 addition & 2 deletions src/config/normalize/lib/wild_wild_path_utils/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { exclude } from './exclude.js'
export { map } from './map.js'
export { pick, include } from './pick.js'
export { pick, include, exclude } from './include.js'
28 changes: 0 additions & 28 deletions src/config/normalize/lib/wild_wild_path_utils/pick.js

This file was deleted.

0 comments on commit e9edefa

Please sign in to comment.