forked from Yomguithereal/mnemonist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default-weak-map.js
108 lines (94 loc) · 2.3 KB
/
default-weak-map.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
/**
* Mnemonist DefaultWeakMap
* =========================
*
* JavaScript implementation of a default weak map that will return a constructed
* value any time one tries to access an non-existing key. It is similar to
* DefaultMap but uses ES6 WeakMap that only holds weak reference to keys.
*/
/**
* DefaultWeakMap.
*
* @constructor
*/
function DefaultWeakMap(factory) {
if (typeof factory !== 'function')
throw new Error('mnemonist/DefaultWeakMap.constructor: expecting a function.');
this.items = new WeakMap();
this.factory = factory;
}
/**
* Method used to clear the structure.
*
* @return {undefined}
*/
DefaultWeakMap.prototype.clear = function() {
// Properties
this.items = new WeakMap();
};
/**
* Method used to get the value set for given key. If the key does not exist,
* the value will be created using the provided factory.
*
* @param {any} key - Target key.
* @return {any}
*/
DefaultWeakMap.prototype.get = function(key) {
var value = this.items.get(key);
if (typeof value === 'undefined') {
value = this.factory(key);
this.items.set(key, value);
}
return value;
};
/**
* Method used to get the value set for given key. If the key does not exist,
* a value won't be created.
*
* @param {any} key - Target key.
* @return {any}
*/
DefaultWeakMap.prototype.peek = function(key) {
return this.items.get(key);
};
/**
* Method used to set a value for given key.
*
* @param {any} key - Target key.
* @param {any} value - Value.
* @return {DefaultMap}
*/
DefaultWeakMap.prototype.set = function(key, value) {
this.items.set(key, value);
return this;
};
/**
* Method used to test the existence of a key in the map.
*
* @param {any} key - Target key.
* @return {boolean}
*/
DefaultWeakMap.prototype.has = function(key) {
return this.items.has(key);
};
/**
* Method used to delete target key.
*
* @param {any} key - Target key.
* @return {boolean}
*/
DefaultWeakMap.prototype.delete = function(key) {
return this.items.delete(key);
};
/**
* Convenience known methods.
*/
DefaultWeakMap.prototype.inspect = function() {
return this.items;
};
if (typeof Symbol !== 'undefined')
DefaultWeakMap.prototype[Symbol.for('nodejs.util.inspect.custom')] = DefaultWeakMap.prototype.inspect;
/**
* Exporting.
*/
module.exports = DefaultWeakMap;