Created
August 9, 2017 20:53
-
-
Save maxired/abb945a832495f325e7dce205b7deb9c to your computer and use it in GitHub Desktop.
Minimal implementation of an immutable set
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
const setPath = ([key, ...next], value, obj) => { | |
if (next.length === 0) { | |
return {...obj, [key]: value }; | |
} | |
return {...obj, [key]: setPath(next, value, obj[key]) }; | |
}; | |
const set = (path, value, obj) => setPath(path.split('.'), value, obj); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this handle bracket notation? Great work BTW!