-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
263 lines (185 loc) · 4.79 KB
/
index.js
File metadata and controls
263 lines (185 loc) · 4.79 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// A simple dictionary prototype for JavaScript, avoiding common object pitfalls
// and offering some handy convenience methods.
//
/* global module, require, window */
var prefix = "string-dict_";
function makeKey (k) {
return prefix + k;
}
function revokeKey (k) {
return k.replace(new RegExp(prefix), "");
}
function Dict (content) {
var key;
this.clear();
if (content) {
for (key in content) {
this.set(key, content[key]);
}
}
}
Dict.prototype.clear = function () {
this.items = {};
this.count = 0;
};
Dict.prototype.length = function () {
return this.count;
};
Dict.prototype.set = function (k, value) {
var key = makeKey(k);
if (!k) {
throw new Error("Dictionary keys cannot be falsy.");
}
if (this.has(key)) {
this.remove(key);
}
this.items[key] = value;
this.count += 1;
return this;
};
Dict.prototype.get = function (k) {
var key = makeKey(k);
if (!this.items.hasOwnProperty(key)) {
return undefined;
}
return this.items[key];
};
//
// The same as .get(), but throws when the key doesn't exist.
// This can be useful if you want to use a dict as some sort of registry.
//
Dict.prototype.require = function (key) {
if (!this.has(key)) {
throw new Error("Required key '" + key + "' does not exist.");
}
return this.get(key);
};
Dict.prototype.remove = function (k) {
var key = makeKey(k);
if (this.has(k)) {
delete this.items[key];
this.count -= 1;
}
return this;
};
Dict.prototype.has = function (k) {
var key = makeKey(k);
return this.items.hasOwnProperty(key);
};
Dict.prototype.forEach = function (fn) {
if (!fn || typeof fn !== "function") {
throw new Error("Argument 1 is expected to be of type function.");
}
for (var key in this.items) {
fn(this.items[key], revokeKey(key), this);
}
return this;
};
Dict.prototype.filter = function (fn) {
var matches = new Dict();
this.forEach(function (item, key, all) {
if (fn(item, key, all)) {
matches.set(key, item);
}
});
return matches;
};
Dict.prototype.find = function (fn) {
var value;
this.some(function (item, key, all) {
if (fn(item, key, all)) {
value = item;
return true;
}
return false;
});
return value;
};
Dict.prototype.map = function (fn) {
var mapped = new Dict();
this.forEach(function (item, key, all) {
mapped.set(key, fn(item, key, all));
});
return mapped;
};
Dict.prototype.reduce = function (fn, initialValue) {
var result = initialValue;
this.forEach(function (item, key, all) {
result = fn(result, item, key, all);
});
return result;
};
Dict.prototype.every = function (fn) {
return this.reduce(function (last, item, key, all) {
return last && fn(item, key, all);
}, true);
};
Dict.prototype.some = function (fn) {
for (var key in this.items) {
if (fn(this.items[key], revokeKey(key), this)) {
return true;
}
}
return false;
};
//
// Returns an array containing the dictionary's keys.
//
Dict.prototype.keys = function () {
var keys = [];
this.forEach(function (item, key) {
keys.push(key);
});
return keys;
};
//
// Returns the dictionary's values in an array.
//
Dict.prototype.values = function () {
var values = [];
this.forEach(function (item) {
values.push(item);
});
return values;
};
//
// Creates a normal JS object containing the contents of the dictionary.
//
Dict.prototype.toObject = function () {
var jsObject = {};
this.forEach(function (item, key) {
jsObject[key] = item;
});
return jsObject;
};
//
// Creates another dictionary with the same contents as this one.
//
Dict.prototype.clone = function () {
var clone = new Dict();
this.forEach(function (item, key) {
clone.set(key, item);
});
return clone;
};
//
// Adds the content of another dictionary to this dictionary's content.
//
Dict.prototype.addMap = function (otherMap) {
var self = this;
otherMap.forEach(function (item, key) {
self.set(key, item);
});
return this;
};
//
// Returns a new map which is the result of joining this map
// with another map. This map isn't changed in the process.
// The keys from otherMap will replace any keys from this map that
// are the same.
//
Dict.prototype.join = function (otherMap) {
return this.clone().addMap(otherMap);
};
module.exports = Dict;