Skip to content

Instantly share code, notes, and snippets.

@maxired
Created August 9, 2017 20:53
Show Gist options
  • Save maxired/abb945a832495f325e7dce205b7deb9c to your computer and use it in GitHub Desktop.
Save maxired/abb945a832495f325e7dce205b7deb9c to your computer and use it in GitHub Desktop.
Minimal implementation of an immutable set
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);
@zomars
Copy link

zomars commented Apr 1, 2019

Does this handle bracket notation? Great work BTW!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment