Skip to content

Commit

Permalink
add option to compute set call of plug
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianMairinger committed Oct 13, 2023
1 parent 6ef0b6b commit da85d91
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions app/src/circClone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ export function uniqueMatch(f: (a: unknown) => boolean) {



export function pluck(ob: any, path: KeyChain, setTo?: unknown) {
export function pluck(ob: any, path: KeyChain, setTo: ((val: unknown) => unknown), computeSet: true)
export function pluck(ob: any, path: KeyChain, setTo?: unknown, computeSet?: false)
export function pluck(ob: any, path: KeyChain, setTo?: unknown | ((val: unknown) => unknown), computeSet?: boolean) {
let cur = ob
const setToIsUnset = setTo === undefined
for (let i = 0; i < (path.length - (setToIsUnset ? 0 : 1)); i++) {
Expand All @@ -171,10 +173,10 @@ export function pluck(ob: any, path: KeyChain, setTo?: unknown) {
}
if (setToIsUnset) return cur
else {
if (path.length === 0) return setTo
if (path.length === 0) return computeSet ? (setTo as Function)(ob) : setTo
else {
const pathFragment = path[path.length - 1]
if (cur[pathFragment] === undefined || Object.hasOwn(cur, pathFragment)) cur[pathFragment] = setTo
if (cur[pathFragment] === undefined || Object.hasOwn(cur, pathFragment)) cur[pathFragment] = computeSet ? (setTo as Function)(cur[pathFragment]) : setTo
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ prototype poisoning protection
return ob
}
Expand Down

0 comments on commit da85d91

Please sign in to comment.