-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
226 lines (184 loc) · 4.56 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
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
"use strict";
/* global module, define */
function mapEach(map, operation){
var keys = map.keys();
var next;
while(!(next = keys.next()).done) {
operation(map.get(next.value), next.value, map);
}
}
var Multimap = (function() {
var mapCtor;
if (typeof Map !== 'undefined') {
mapCtor = Map;
if (!Map.prototype.keys) {
Map.prototype.keys = function() {
var keys = [];
this.forEach(function(item, key) {
keys.push(key);
});
return keys;
};
}
}
function Multimap(iterable) {
var self = this;
self._map = mapCtor;
if (Multimap.Map) {
self._map = Multimap.Map;
}
self._ = self._map ? new self._map() : {};
if (iterable) {
iterable.forEach(function(i) {
self.set(i[0], i[1]);
});
}
}
/**
* @param {Object} key
* @return {Array} An array of values, undefined if no such a key;
*/
Multimap.prototype.get = function(key) {
return this._map ? this._.get(key) : this._[key];
};
/**
* @param {Object} key
* @param {Object} val...
*/
Multimap.prototype.set = function(key, val) {
var args = Array.prototype.slice.call(arguments);
key = args.shift();
var entry = this.get(key);
if (!entry) {
entry = [];
if (this._map)
this._.set(key, entry);
else
this._[key] = entry;
}
Array.prototype.push.apply(entry, args);
return this;
};
/**
* @param {Object} key
* @param {Object=} val
* @return {boolean} true if any thing changed
*/
Multimap.prototype.delete = function(key, val) {
if (!this.has(key))
return false;
if (arguments.length == 1) {
this._map ? (this._.delete(key)) : (delete this._[key]);
return true;
} else {
var entry = this.get(key);
var idx = entry.indexOf(val);
if (idx != -1) {
entry.splice(idx, 1);
return true;
}
}
return false;
};
/**
* @param {Object} key
* @param {Object=} val
* @return {boolean} whether the map contains 'key' or 'key=>val' pair
*/
Multimap.prototype.has = function(key, val) {
var hasKey = this._map ? this._.has(key) : this._.hasOwnProperty(key);
if (arguments.length == 1 || !hasKey)
return hasKey;
var entry = this.get(key) || [];
return entry.indexOf(val) != -1;
};
/**
* @return {Array} all the keys in the map
*/
Multimap.prototype.keys = function() {
if (this._map)
return makeIterator(this._.keys());
return makeIterator(Object.keys(this._));
};
/**
* @return {Array} all the values in the map
*/
Multimap.prototype.values = function() {
var vals = [];
this.forEachEntry(function(entry) {
Array.prototype.push.apply(vals, entry);
});
return makeIterator(vals);
};
/**
*
*/
Multimap.prototype.forEachEntry = function(iter) {
mapEach(this, iter);
};
Multimap.prototype.forEach = function(iter) {
var self = this;
self.forEachEntry(function(entry, key) {
entry.forEach(function(item) {
iter(item, key, self);
});
});
};
Multimap.prototype.clear = function() {
if (this._map) {
this._.clear();
} else {
this._ = {};
}
};
Object.defineProperty(
Multimap.prototype,
"size", {
configurable: false,
enumerable: true,
get: function() {
var total = 0;
mapEach(this, function(value){
total += value.length;
});
return total;
}
});
Object.defineProperty(
Multimap.prototype,
"count", {
configurable: false,
enumerable: true,
get: function() {
return this._.size;
}
});
var safariNext;
try{
safariNext = new Function('iterator', 'makeIterator', 'var keysArray = []; for(var key of iterator){keysArray.push(key);} return makeIterator(keysArray).next;');
}catch(error){
// for of not implemented;
}
function makeIterator(iterator){
if(Array.isArray(iterator)){
var nextIndex = 0;
return {
next: function(){
return nextIndex < iterator.length ?
{value: iterator[nextIndex++], done: false} :
{done: true};
}
};
}
// Only an issue in safari
if(!iterator.next && safariNext){
iterator.next = safariNext(iterator, makeIterator);
}
return iterator;
}
return Multimap;
})();
if(typeof exports === 'object' && module && module.exports)
module.exports = Multimap;
else if(typeof define === 'function' && define.amd)
define(function() { return Multimap; });