-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (42 loc) · 1023 Bytes
/
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
/**
* An array of blacklisted property keys.
*
* @type {string[]}
*/
const blackList = ['clear', 'delete', 'set']
/**
* A handler object for the Proxy.
*
* @type {object}
*/
const handler =
{
/**
* A trap for getting property values.
*
* @param {object} target - The target object.
* @param {string} propertyKey - The property key to get.
*
* @returns {*} - The property value.
* @throws {SyntaxError} - If the property key is blacklisted.
*/
get(target, propertyKey)
{
if(blackList.includes(propertyKey))
throw new SyntaxError(`${propertyKey} not allowed`)
let result = Reflect.get(target, propertyKey)
if(result instanceof Function) result = result.bind(target)
return result
}
}
/**
* Creates a new Proxy object with the given target object and handler.
*
* @param {object} target - The target object to proxy.
*
* @returns {object} - The new Proxy object.
*/
module.exports = function ReadonlyMap(target)
{
return new Proxy(target, handler)
}