-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (41 loc) · 1.21 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use strict";
function deepLock(obj, options) {
var action = "freeze";
if (options && options.action) {
switch (options.action) {
case "freeze":
case "seal":
case "preventExtensions":
action = options.action;
break;
default:
throw new Error(`Options action can't be ${options.action}`);
}
}
return lock(obj, action);
}
function lock(obj, action, locked = new Set()) {
if (locked.has(obj)) return obj; // Prevent circular reference
Object[action](obj);
locked.add(obj);
// In strict mode obj.caller and obj.arguments are non-deletable properties which throw when set or retrieved
if (obj === Function.prototype) return obj;
const keys = Object.getOwnPropertyNames(obj);
// Not supported in IE
if (Object.getOwnPropertySymbols) {
keys.push(...Object.getOwnPropertySymbols(obj));
}
keys.forEach((prop) => {
if (
Object.hasOwnProperty.call(obj, prop) &&
obj[prop] !== null &&
(typeof obj[prop] === "object" || typeof obj[prop] === "function") &&
!ArrayBuffer.isView(obj[prop])
) {
lock(obj[prop], action, locked);
}
});
return obj;
}
module.exports = deepLock;
module.exports.default = deepLock;