-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.js
78 lines (64 loc) · 1.33 KB
/
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
const defineProperty = Object.defineProperty;
/**
* Initializes an instance of a Map
* @constructs Map
*/
function Map() {
/**
* Object to store the values of the Map
* @name Map#map
* @type {Object}
*/
this.map;
defineProperty(this, 'map', {
value: Object.create(null) /* default */
});
/**
* Size of the map
* @name Map#size
* @type {number}
*/
this.size;
defineProperty(this, 'size', {
get() { return Object.keys(this.map).length; }
});
return this;
}
/**
* Sets a pair of key, value in the Map
* @function Map#set
* @param {*} key
* @param {*} value
*/
Map.prototype.set = function (key, value) {
this.map[String(key)] = value;
return this;
};
/**
* Tells if the Map has a key
* @function Map#has
* @param {*} key Key to look for
*/
Map.prototype.has = function (key) {
return !!this.map[String(key)];
};
/**
* Deletes a key from the Map
* @function Map#delete
* @param {*} key Key to be deleted
*/
Map.prototype.delete = function (key) {
const result = this.has(key);
delete this.map[String(key)];
return result;
};
/**
* Returns the value of a key in the Map
* Throws an exception if the key doesn't exist
* @function Map#get
* @param {*} key Key to be retrieved.
*/
Map.prototype.get = function (key) {
return this.map[String(key)];
};
module.exports = Map;