Last active
October 28, 2021 06:41
-
-
Save hamzakaya/c046940d3584055d42f55b66e0486acb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function objSet(source, keys, update) { | |
keys.split && (keys = keys.split('.')); | |
let next = copy(source), | |
last = next, | |
i = 0, | |
l = keys.length; | |
for (; i < l; i++) { | |
last = last[keys[i]] = | |
i === l - 1 | |
? update && !!update.call | |
? update(last[keys[i]]) | |
: update | |
: copy(last[keys[i]]); | |
} | |
return next; | |
} | |
function copy(source) { | |
let to = source && !!source.pop ? [] : {}; | |
for (let i in source) to[i] = source[i]; | |
return to; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment