-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
175 lines (137 loc) · 4.31 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const proxy = (obj = {}, path = [], originalObj = null) => {
if (typeof obj !== 'object') {
throw `obj-type-non-object`
}
if (typeof obj.$meta === 'undefined' && originalObj === null) {
obj.$meta = {
'frozen': [],
'type-locked': []
}
}
return new Proxy(obj, {
get: function (target, property) {
const value = target[property]
const _path = [ ...path, property ]
if (originalObj === null) {
originalObj = obj
}
/*
* Determine if the user is trying to call $freeze() or $lock() and return the
* corresponding function so it is invoked.
*/
switch(property) {
case '$freeze':
return freeze(_path, originalObj.$meta)
case '$lock':
return lock(_path, originalObj.$meta)
case '$meta':
throw 'meta-is-reserved-property'
}
/*
* Proxy methods ($lock and $freeze) are added to primitive types and functions.
* e.g., a user can call .$lock() and .$freeze() on a string.
*/
switch(typeof value) {
case 'object':
return proxy(value, [ ...path, property ], originalObj)
case 'string':
return createProxyString(_path, originalObj.$meta, value)
case 'number':
return createProxyNumber(_path, originalObj.$meta, value)
case 'boolean':
return createProxyBoolean(_path, originalObj.$meta, value)
case 'function':
return addProxyMethodsToFunction(_path, originalObj.$meta, value)
default:
return value
}
},
set: function (target, property, value) {
const _path = [ ...path, property ]
if (originalObj === null) {
originalObj = obj
}
if (isFrozen(_path, originalObj.$meta)) {
throw `property-frozen`
} else if (isTypeLocked(_path, originalObj.$meta) && typeof value !== typeof target[property]) {
throw `property-type-locked`
}
target[property] = value
}
})
}
const freeze = (path, $meta) => {
if (path.length === 0) {
throw `root-cannot-be-frozen`
}
$meta['frozen'] = [
...$meta['frozen'],
path.slice(0, path.length).join('.')
]
}
const lock = (path, $meta) => {
if (path.length === 0) {
throw `root-cannot-be-type-locked`
}
$meta['type-locked'] = [
...$meta['type-locked'],
path.slice(0, path.length).join('.')
]
}
const isFrozen = (path, $meta) => {
for (let i = 0; i < path.length; i++) {
const isMatch = $meta['frozen'].find((p) => p === path.slice(0, i + 1).join('.')) !== undefined
if (isMatch) {
return true
}
}
return false
}
const isTypeLocked = (path, $meta) => {
for (let i = 0; i < path.length; i++) {
const isMatch = $meta['type-locked'].find((p) => p === path.slice(0, i + 1).join('.')) !== undefined
if (isMatch) {
return true
}
}
return false
}
const createProxyString = (path, $meta, value) => {
const ProxyString = class extends String {
$freeze() {
freeze(path, $meta)
}
$lock() {
lock(path, $meta)
}
}
return new ProxyString(value)
}
const createProxyBoolean = (path, $meta, value) => {
const ProxyBoolean = class extends Boolean {
$freeze() {
freeze(path, $meta)
}
$lock() {
lock(path, $meta)
}
}
return new ProxyBoolean(value)
}
const createProxyNumber = (path, $meta, value) => {
const ProxyNumber = class extends Number {
$freeze() {
freeze(path, $meta)
}
$lock() {
lock(path, $meta)
}
}
return new ProxyNumber(value)
}
const addProxyMethodsToFunction = (path, $meta, value) => {
value.$freeze = () => freeze(path, $meta)
value.$lock = () => lock(path, $meta)
return value
}
module.exports = (obj) => proxy(obj)