Created
October 6, 2016 16:24
-
-
Save kirilloid/04f37b3b83276dc53b794f87bfb5b148 to your computer and use it in GitHub Desktop.
immutable
This file contains hidden or 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 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