Skip to content

Commit

Permalink
Improve set()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent 6747024 commit 6b1897b
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/config/normalize/lib/wild_wild_path/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import { setArray } from '../../../../utils/set.js'
import { handleMissingValue } from './iterate/expand.js'
import { iterate } from './iterate/main.js'

// Set a value to one or multiple properties in `target` using a query string
// Set a value to one or multiple properties in `target` using a query string.
// Uses `iterate()` to keep memory consumption low.
export const set = function (target, queryOrPath, value) {
const entries = iterate(target, queryOrPath)
return entries.reduce(
(targetA, entry) => setEntry(targetA, entry.path, value, 0),
target,
)
// eslint-disable-next-line fp/no-let
let newTarget = target

// eslint-disable-next-line fp/no-loops
for (const { path } of iterate(target, queryOrPath)) {
// eslint-disable-next-line fp/no-mutation
newTarget = setEntry(newTarget, path, value, 0)
}

return newTarget
}

// Use positional arguments for performance
Expand Down

0 comments on commit 6b1897b

Please sign in to comment.