Skip to content

Commit

Permalink
Add remove()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 6, 2022
1 parent 8e228fc commit 8429653
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/config/normalize/lib/star_dot_path/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export const handleMissingValue = function (value, token) {
return valueA
}

export const isDefined = function (value, token) {
const tokenType = getObjectTokenType(token)
return tokenType.isDefined(value)
}

// Compute all entries properties from the basic ones
export const normalizeEntry = function ({ value, path }) {
const query = serialize(path)
Expand Down
2 changes: 1 addition & 1 deletion src/config/normalize/lib/star_dot_path/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export { list, get, has } from './get.js'
export { equals, parent } from './parsing/compare.js'
export { serialize } from './parsing/serialize.js'
export { parse } from './parsing/parse.js'
export { set } from './set.js'
export { set, remove } from './set.js'
40 changes: 34 additions & 6 deletions src/config/normalize/lib/star_dot_path/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import omit from 'omit.js'

import { setArray } from '../../../../utils/set.js'

import { listEntries, handleMissingValue } from './entries.js'
import { listEntries, handleMissingValue, isDefined } from './entries.js'
import { parse } from './parsing/parse.js'

// Set a value to one or multiple properties in `target` using a query string
Expand Down Expand Up @@ -43,6 +43,38 @@ const setValue = function (target, key, childValue) {
: { ...target, [key]: childValue }
}

// Same as `set()` but removing a value
export const remove = function (target, queryOrPath) {
const path = parse(queryOrPath)
const entries = listEntries(target, path)
return entries.some(hasRootPath)
? undefined
: entries.reduce(
(targetA, entry) => removeEntry(targetA, entry.path, 0),
target,
)
}

const hasRootPath = function ({ path }) {
return path.length === 0
}

const removeEntry = function (target, path, index) {
const key = path[index]

if (!isDefined(target, key)) {
return target
}

if (index === path.length - 1) {
return removeValue(target, key)
}

const childTarget = target[key]
const childValue = removeEntry(childTarget, path, index + 1)
return setValue(target, key, childValue)
}

const removeValue = function (target, key) {
return Array.isArray(target, key)
? removeArrayValue(target, key)
Expand All @@ -63,9 +95,5 @@ const isUndefined = function (item) {
}

const removeObjectValue = function (target, key) {
if (!(key in target)) {
return target
}

return omit.default(target, [key])
return key in target ? omit.default(target, [key]) : target
}

0 comments on commit 8429653

Please sign in to comment.