-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.js
More file actions
62 lines (46 loc) · 1.39 KB
/
HashMap.js
File metadata and controls
62 lines (46 loc) · 1.39 KB
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
// HashMap.js
class HashMap {
constructor() {
this.map = {};
}
add(id, obj) {
//logger.debug("HashMap = Adding item to HashMap [" + id + "] with value [" + JSON.stringify(obj) + "]");
logger.debug("HashMap = Adding item to HashMap [" + id + "]");
this.map[id] = obj;
}
get(id,startsWith) {
if(startsWith==true)
{
logger.debug("HashMap = Getting Item starting with [" + id + "]");
// Find a key that starts with the search term
const foundKey = Object.keys(this.map).find(key => key.startsWith(id));
if (foundKey) {
logger.debug("HashMap = Found key starting with [" + id + "]: [" + foundKey + "]");
return this.map[foundKey];
}
return undefined; // Return undefined if no match is found
}
else {
logger.debug("HashMap = Getting Item Matching [" + id + "]");
return this.map[id];
}
}
remove(id) {
delete this.map[id];
}
has(id) {
return this.map.hasOwnProperty(id);
}
keys() {
return Object.keys(this.map);
}
contains(searchValue) {
var item = this.get(searchValue);
if(item!==undefined && item !== null)return true;
return false;
}
size() {
return Object.keys(this.map).length;
}
}
module.exports = HashMap;