-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (30 loc) · 1.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';
module.exports = function simpleDeepFreeze(object) {
// No freezing in production (for better performance).
try {
if (process.env.NODE_ENV === 'production') {
return object;
}
} catch (e) {}
// When already frozen, we assume its children are frozen (for better performance).
// This should be true if you always use `simpleDeepFreeze` to freeze objects,
// which is why you should have a linter rule that prevents you from using
// `Object.freeze` standalone.
//
// Note that Object.isFrozen will also return `true` for primitives (numbers,
// strings, booleans, undefined, null), so there is no need to check for
// those explicitly.
if (Object.isFrozen(object)) {
return object;
}
if (!Array.isArray(object) && Object.getPrototypeOf(object) !== Object.getPrototypeOf({})) {
throw new Error('simpleDeepFreeze only supports plain objects, arrays, and primitives');
}
// At this point we know that we're dealing with either an array or plain object, so
// just freeze it and recurse on its values.
Object.freeze(object);
Object.keys(object).forEach(function(key) {
simpleDeepFreeze(object[key]);
});
return object;
};