-
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
5 changed files
with
90 additions
and
97 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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)) | ||
} |
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 |
---|---|---|
@@ -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' |
This file was deleted.
Oops, something went wrong.