-
Notifications
You must be signed in to change notification settings - Fork 4
/
Map.js
63 lines (47 loc) · 1.41 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
(function(define) {
define(['./hash'], function(hash) {
function Map(hasher) {
this._items = {};
this.length = 0;
this._hasher = hasher || hash.base;
}
Map.prototype = {
forEach: function(forEachFunc) {
var items = this._items;
for(var key in items) {
forEachFunc(key, items[key]);
}
// TODO: return this?
},
add: function(key, value) {
var items = this._items;
if(!this.contains(key)) {
items[key] = value;
++this.length;
}
// TODO: return this?
return this.length;
},
contains: function(key) {
key = this._hasher(key);
return key in this._items;
},
get: function(key) {
return this._items[this._hasher(key)];
},
remove: function(key) {
var items, removed;
items = this._items;
key = this._hasher(key);
if (key in items) {
removed = items[key];
delete items[key];
--this.length;
}
// TODO: return this?
return removed;
}
};
return Map;
});
})(typeof define != 'undefined' ? define : function(deps, factory) { module.exports = factory.apply(this, deps.map(require)); });