Skip to content

Commit

Permalink
Add exclude()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 6, 2022
1 parent 90e56d8 commit b58938a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/config/normalize/lib/star_dot_path_utils/main.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { map } from './map.js'
export { map, exclude } from './map.js'
export { pick } from './pick.js'
13 changes: 12 additions & 1 deletion src/config/normalize/lib/star_dot_path_utils/map.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// We split the core methods of `star_dot_path` to keep it small, and provide
// additional utilities built on top of it.
import { list, get, set } from '../star_dot_path/main.js'
import { list, get, set, remove } from '../star_dot_path/main.js'

// Map values matching a query
export const map = function (target, queryOrPath, mapFunc) {
Expand All @@ -13,3 +13,14 @@ const mapEntry = function (mapFunc, target, { path, query }) {
const mappedValue = mapFunc({ path, query, value })
return set(target, path, mappedValue)
}

// Remove values matching a query
export const exclude = function (target, queryOrPath, condition) {
const entries = list(target, queryOrPath)
return entries.reduceRight(excludeEntry.bind(undefined, condition), target)
}

const excludeEntry = function (condition, target, { path, query }) {
const value = get(target, path)
return condition({ path, query, value }) ? remove(target, path) : target
}

0 comments on commit b58938a

Please sign in to comment.