Skip to content

Instantly share code, notes, and snippets.

@hamzakaya
Last active October 28, 2021 06:41
Show Gist options
  • Save hamzakaya/c046940d3584055d42f55b66e0486acb to your computer and use it in GitHub Desktop.
Save hamzakaya/c046940d3584055d42f55b66e0486acb to your computer and use it in GitHub Desktop.
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