Last active
August 29, 2015 14:27
-
-
Save corymartin/ad560de3cf80204a2c14 to your computer and use it in GitHub Desktop.
Mutates an object hash into an object of getters and setters where the setters - if used - will throw an error.
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
function immutabilly(props) { | |
Object.keys(props).forEach(key => { | |
const prop = props[key]; | |
if (prop === Object(prop)) { | |
immutabilly(prop); | |
} | |
Object.defineProperty(props, key, { | |
enumerable: true, | |
get: () => prop, | |
set: () => {throw new Error(`Illegal assignment to immutable property '${key}'`);}, | |
}); | |
}); | |
return props; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment