Skip to content

Instantly share code, notes, and snippets.

@pertrai1
Created January 2, 2020 21:03
Show Gist options
  • Save pertrai1/8372aa8f8846689f94b3cc7f0db0e3b6 to your computer and use it in GitHub Desktop.
Save pertrai1/8372aa8f8846689f94b3cc7f0db0e3b6 to your computer and use it in GitHub Desktop.
Deep Freeze
function deepFreeze(value) {
if (Array.isArray(value)) {
for (const element of value) {
deepFreeze(element);
}
Object.freeze(value);
} else if (typeof value === 'object' && value !== null) {
for (const v of Object.values(value)) {
deepFreeze(v);
}
Object.freeze(value);
} else {
// Nothing to do: primitive values are already immutable
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment