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 3f3da0d commit 43ddbd6
Showing 1 changed file with 2 additions and 25 deletions.
27 changes: 2 additions & 25 deletions src/config/normalize/lib/wild_wild_path/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,11 @@ import omit from 'omit.js'
import { setArray } from '../../../../utils/set.js'

import { handleMissingValue } from './iterate/expand.js'
import { iterate } from './iterate/main.js'
import { parent } from './parsing/compare.js'
import { setValue } from './set.js'
import { reduceParents, setValue } from './set.js'

// Same as `set()` but removing a value
export const remove = function (target, queryOrPath) {
// eslint-disable-next-line fp/no-let
let newTarget = target

const paths = []

// eslint-disable-next-line fp/no-loops
for (const { path } of iterate(target, queryOrPath)) {
// eslint-disable-next-line max-depth
if (!parentIsRemoved(paths, path)) {
// eslint-disable-next-line fp/no-mutating-methods
paths.push(path)
// eslint-disable-next-line fp/no-mutation
newTarget = removeAnyEntry(newTarget, path, 0)
}
}

return newTarget
}

// If both a parent and a child property are removed, the parent prevails
const parentIsRemoved = function (paths, path) {
return paths.some((previousPath) => parent(previousPath, path))
return reduceParents(target, queryOrPath, removeAnyEntry)
}

const removeAnyEntry = function (target, path, index) {
Expand Down

0 comments on commit 43ddbd6

Please sign in to comment.