-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (144 loc) · 4.15 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
/**
* An extended Map that facilitates storing things at a given 'namespace'
* very much like in C#, Java, C, or any other language which has the
* concept of namespaces.
*
* Example, lets say you wanted to store Foo, Bar and Baz at the namespace
* com.fake.things. You could do that! when pulling the value for things at
* the namespace com.fake.things you would receive your three things.
*/
class NamespaceMap {
/**
* Constructs a new NamespaceMap
*
* @param {Map} namespace A map to seed the namespace map with.
*/
constructor(namespace) {
/**
* The root of the NamespaceMaps namespace.
* @type Map
*/
this.namespace = namespace instanceof Map ? namespace : new Map();
}
/**
* Add something to the given namespace. The namespace is created if it
* does not already exist.
*
* @param {any} value The thing we are placing within the namespace.
* @param {string} namespace The namespace where value will be placed.
* @returns {NamespaceMap} Returns this instance of NamespaceMap.
*/
add(value, namespace) {
// TODO: Ensure no # are used in namespace.
// FIXME: Restrict namespaces to a-Z0-9 and .
const keys = namespace.split('.');
let currentMap = this.namespace;
for (const [index, entry] of keys.entries()) {
const end = index === keys.length - 1;
let nextMap;
if (!currentMap.has(entry)) {
nextMap = new Map();
if (end) {
nextMap.set('#', value);
}
currentMap.set(entry, nextMap);
} else {
nextMap = currentMap.get(entry);
if (end) {
nextMap.set('#', value);
}
}
if (end) {
break;
}
currentMap = nextMap;
}
return this;
}
/**
* Gets what is stored at a given namespace.
*
* @param {string} namespace The namespace of where to locate the desired
* thing.
* @returns {any|null} Returns either the value at the given namespace or null
* if it could not be found.
*/
get(namespace) {
const keys = namespace.split('.');
let currentMap = this.namespace;
if (!currentMap.has(keys[0])) {
return null;
}
for (const [index, entry] of keys.entries()) {
const end = index === keys.length - 1;
if (end && entry === '*') {
return new NamespaceMap(currentMap);
}
if (currentMap instanceof Map) {
const nextMap = currentMap.get(entry);
if (nextMap) {
currentMap = nextMap;
if (end) {
if (currentMap instanceof Map) {
const value = currentMap.get('#');
if (value) {
return value;
}
return new NamespaceMap(currentMap);
}
}
} else {
// Found nothing at that namespace.
return null;
}
} else if (entry !== '#') {
return null;
}
}
return currentMap;
}
/**
* Delete the value at the given namespace and all empty nodes.
*
* @param {string} namespace The namespace of where to find the
* thing to delete.
* @returns {NamespaceMap} Returns this instance of NamespaceMap.
*/
delete(namespace) {
// TODO: Ensure no # are used in namespace.
// FIXME: Restrict namespaces to a-Z0-9 and .
const keys = namespace.split('.');
let currentMap = this.namespace;
if (!currentMap.has(keys[0])) {
return this;
}
const processedNodes = [];
for (const [index, entry] of keys.entries()) {
const end = index === keys.length - 1;
const nextMap = currentMap.get(entry);
if (!end) {
if (!nextMap || !(nextMap instanceof Map)) {
break;
}
currentMap = nextMap;
processedNodes.push(entry);
continue;
}
if (nextMap instanceof Map) {
nextMap.delete('#');
if (nextMap.size === 0) {
currentMap.delete(entry);
processedNodes.push(entry);
}
} else {
currentMap.delete(entry);
}
if (currentMap.size === 0) {
this.delete(processedNodes.join('.'));
break;
}
}
return this;
}
}
export default NamespaceMap;