This repository has been archived by the owner on Mar 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
es6-map-shim.js
255 lines (241 loc) · 8.64 KB
/
es6-map-shim.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
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
/**
* Copyright 2012 Eric Wendelin - MIT License
*
* es6-map-shim.js is a DESTRUCTIVE shim that follows the latest Map specification
* as closely as possible. It is destructive in the sense that it overrides native implementations.
*
* IMPORTANT: Currently, get(), set(), has() and delete() are all O(n) operations.
* Normally, they would be O(1). Therefore it is NOT recommended to use this with
* a large dataset or in any production environment.
*
* This library assumes ES5 functionality: Object.create, Object.defineProperty,
* Array.indexOf, Function.bind and others.
*/
(function(module) {
function Map(iterable) {
var _items = [];
var _keys = [];
var _values = [];
// Object.is polyfill, courtesy of @WebReflection
var is = Object.is || function(a, b) {
return a === b ?
a !== 0 || 1 / a == 1 / b :
a != a && b != b;
};
// More reliable indexOf, courtesy of @WebReflection
var betterIndexOf = function(value) {
if(value != value || value === 0) {
for(var i = this.length; i-- && !is(this[i], value););
} else {
i = [].indexOf.call(this, value);
}
return i;
};
/**
* MapIterator used for iterating over all entries in given map.
*
* @param map {Map} to iterate
* @param kind {String} identifying what to yield. Possible values
* are 'keys', 'values' and 'keys+values'
* @constructor
*/
var MapIterator = function MapIterator(map, kind) {
var _index = 0;
return Object.create({}, {
next: {
value: function() {
// check if index is within bounds
if (_index < map.items().length) {
switch(kind) {
case 'keys': return map.keys()[_index++];
case 'values': return map.values()[_index++];
case 'keys+values': return [].slice.call(map.items()[_index++]);
default: throw new TypeError('Invalid iterator type');
}
}
// TODO: make sure I'm interpreting the spec correctly here
throw new Error('Stop Iteration');
}
},
iterator: {
value: function() {
return this;
}
},
toString: {
value: function() {
return '[object Map Iterator]';
}
}
});
};
var _set = function(key, value) {
// check if key exists and overwrite
var index = betterIndexOf.call(_keys, key);
if (index > -1) {
_items[index][1] = value;
_values[index] = value;
} else {
_items.push([key, value]);
_keys.push(key);
_values.push(value);
}
};
var setItem = function(item) {
if (item.length !== 2) {
throw new TypeError('Invalid iterable passed to Map constructor');
}
_set(item[0], item[1]);
};
// FIXME: accommodate any class that defines an @@iterator method that returns
// an iterator object that produces two element array-like objects
if (Array.isArray(iterable)) {
iterable.forEach(setItem);
} else if (iterable !== undefined) {
throw new TypeError('Invalid Map');
}
return Object.create(MapPrototype, {
/**
* @return {Array} all entries in the Map, in order
*/
items:{
value:function() {
return [].slice.call(_items);
}
},
/**
* @return {Array} all keys in the Map, in order
*/
keys:{
value:function() {
return [].slice.call(_keys);
}
},
/**
* @return {Array} all values in the Map, in order
*/
values:{
value:function() {
return [].slice.call(_values);
}
},
/**
* Given a key, indicate whether that key exists in this Map.
*
* @param key {Object} expected key
* @return {Boolean} true if key in Map
*/
has:{
value:function(key) {
// TODO: double-check how spec reads about null values
var index = betterIndexOf.call(_keys, key);
return index > -1;
}
},
/**
* Given a key, retrieve the value associated with that key (or undefined).
*
* @param key {Object}
* @return {Object} value associated with key or undefined
*/
get:{
value:function(key) {
var index = betterIndexOf.call(_keys, key);
return index > -1 ? _values[index] : undefined;
}
},
/**
* Add or overwrite entry associating key with value. Always returns undefined.
*
* @param key {Object} anything
* @param value {Object} also anything
*/
set:{
value: _set
},
/**
* Return the number of entries in this Map.
*
* @return {Number} number of entries
*/
size:{
get:function() {
return _items.length;
}
},
/**
* Remove all entries in this Map. Returns undefined.
*/
clear:{
value:function() {
_keys.length = _values.length = _items.length = 0;
}
},
/**
* Delete entry with given key, if it exists.
*
* @param key {Object} any possible key
* @return {Boolean} true if an entry was deleted
*/
'delete':{
value:function(key) {
var index = betterIndexOf.call(_keys, key);
if (index > -1) {
_keys.splice(index, 1);
_values.splice(index, 1);
_items.splice(index, 1);
return true;
}
return false;
}
},
/**
* Given a callback function and optional context, invoke the callback on all
* entries in this Map.
*
* @param callbackFn {Function}
*/
forEach:{
value:function(callbackfn /*, thisArg*/) {
if (typeof callbackfn != 'function') {
throw new TypeError('Invalid callback function given to forEach');
}
function tryNext() {
try {
return iter.next();
} catch(e) {
return undefined;
}
}
var iter = this.iterator();
var current = tryNext();
var next = tryNext();
while(current !== undefined) {
callbackfn.apply(arguments[1], [current[1], current[0], this]);
current = next;
next = tryNext();
}
}
},
/**
* Return a MapIterator object for this map.
*/
iterator:{
value: function() {
return new MapIterator(this, 'keys+values');
}
},
toString:{
value: function() {
return '[Object Map]';
}
}
});
}
var notInNode = module == 'undefined';
var window = notInNode ? this : global;
var module = notInNode ? {} : exports;
var MapPrototype = Map.prototype;
Map.prototype = MapPrototype = Map();
window.Map = module.Map = window.Map || Map;
}.call(this, typeof exports));