Skip to content

Instantly share code, notes, and snippets.

@kirilloid
Created October 6, 2016 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirilloid/04f37b3b83276dc53b794f87bfb5b148 to your computer and use it in GitHub Desktop.
Save kirilloid/04f37b3b83276dc53b794f87bfb5b148 to your computer and use it in GitHub Desktop.
immutable
function assingImmutable(obj, toAdd) {
if (!_.isObjectLike(toAdd)) {
return toAdd;
}
key = _.first(path);
if (_.isArray(obj)) {
copy = _.slice(obj);
toAdd.forEach(function (value, idx) {
copy[idx] = assingImmutable(obj[idx], value);
});
return copy;
} else if (_.isObject(obj)) {
copy = _.clone(obj);
Object.keys(toAdd).forEach(function (key) {
copy[key] = assingImmutable(obj[key], toAdd[key]);
});
return copy;
} else {
return toAdd;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment