map.js
10.8 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* @fileoverview
* Implements the Map object.
* @author NHN.
* FE Development Lab <dl_javascript@nhn.com>
*/
'use strict';
var collection = require('./collection');
var type = require('./type');
var array = require('./array');
var browser = require('./browser');
var func = require('./func');
/**
* Using undefined for a key can be ambiguous if there's deleted item in the array,<br>
* which is also undefined when accessed by index.<br>
* So use this unique object as an undefined key to distinguish it from deleted keys.
* @private
* @constant
*/
var _KEY_FOR_UNDEFINED = {};
/**
* For using NaN as a key, use this unique object as a NaN key.<br>
* This makes it easier and faster to compare an object with each keys in the array<br>
* through no exceptional comapring for NaN.
* @private
* @constant
*/
var _KEY_FOR_NAN = {};
/**
* Constructor of MapIterator<br>
* Creates iterator object with new keyword.
* @constructor
* @param {Array} keys - The array of keys in the map
* @param {function} valueGetter - Function that returns certain value,
* taking key and keyIndex as arguments.
* @ignore
*/
function MapIterator(keys, valueGetter) {
this._keys = keys;
this._valueGetter = valueGetter;
this._length = this._keys.length;
this._index = -1;
this._done = false;
}
/**
* Implementation of Iterator protocol.
* @returns {{done: boolean, value: *}} Object that contains done(boolean) and value.
*/
MapIterator.prototype.next = function() {
var data = {};
do {
this._index += 1;
} while (type.isUndefined(this._keys[this._index]) && this._index < this._length);
if (this._index >= this._length) {
data.done = true;
} else {
data.done = false;
data.value = this._valueGetter(this._keys[this._index], this._index);
}
return data;
};
/**
* The Map object implements the ES6 Map specification as closely as possible.<br>
* For using objects and primitive values as keys, this object uses array internally.<br>
* So if the key is not a string, get(), set(), has(), delete() will operates in O(n),<br>
* and it can cause performance issues with a large dataset.
*
* Features listed below are not supported. (can't be implented without native support)
* - Map object is iterable<br>
* - Iterable object can be used as an argument of constructor
*
* If the browser supports full implementation of ES6 Map specification, native Map obejct
* will be used internally.
* @class
* @param {Array} initData - Array of key-value pairs (2-element Arrays).
* Each key-value pair will be added to the new Map
* @memberof tui.util
* @example
* // node, commonjs
* var Map = require('tui-code-snippet').Map;
* @example
* // distribution file, script
* <script src='path-to/tui-code-snippt.js'></script>
* <script>
* var Map = tui.util.Map;
* <script>
*/
function Map(initData) {
this._valuesForString = {};
this._valuesForIndex = {};
this._keys = [];
if (initData) {
this._setInitData(initData);
}
this.size = 0;
}
/* eslint-disable no-extend-native */
/**
* Add all elements in the initData to the Map object.
* @private
* @param {Array} initData - Array of key-value pairs to add to the Map object
*/
Map.prototype._setInitData = function(initData) {
if (!type.isArray(initData)) {
throw new Error('Only Array is supported.');
}
collection.forEachArray(initData, function(pair) {
this.set(pair[0], pair[1]);
}, this);
};
/**
* Returns true if the specified value is NaN.<br>
* For unsing NaN as a key, use this method to test equality of NaN<br>
* because === operator doesn't work for NaN.
* @private
* @param {*} value - Any object to be tested
* @returns {boolean} True if value is NaN, false otherwise.
*/
Map.prototype._isNaN = function(value) {
return typeof value === 'number' && value !== value; // eslint-disable-line no-self-compare
};
/**
* Returns the index of the specified key.
* @private
* @param {*} key - The key object to search for.
* @returns {number} The index of the specified key
*/
Map.prototype._getKeyIndex = function(key) {
var result = -1;
var value;
if (type.isString(key)) {
value = this._valuesForString[key];
if (value) {
result = value.keyIndex;
}
} else {
result = array.inArray(key, this._keys);
}
return result;
};
/**
* Returns the original key of the specified key.
* @private
* @param {*} key - key
* @returns {*} Original key
*/
Map.prototype._getOriginKey = function(key) {
var originKey = key;
if (key === _KEY_FOR_UNDEFINED) {
originKey = undefined; // eslint-disable-line no-undefined
} else if (key === _KEY_FOR_NAN) {
originKey = NaN;
}
return originKey;
};
/**
* Returns the unique key of the specified key.
* @private
* @param {*} key - key
* @returns {*} Unique key
*/
Map.prototype._getUniqueKey = function(key) {
var uniqueKey = key;
if (type.isUndefined(key)) {
uniqueKey = _KEY_FOR_UNDEFINED;
} else if (this._isNaN(key)) {
uniqueKey = _KEY_FOR_NAN;
}
return uniqueKey;
};
/**
* Returns the value object of the specified key.
* @private
* @param {*} key - The key of the value object to be returned
* @param {number} keyIndex - The index of the key
* @returns {{keyIndex: number, origin: *}} Value object
*/
Map.prototype._getValueObject = function(key, keyIndex) { // eslint-disable-line consistent-return
if (type.isString(key)) {
return this._valuesForString[key];
}
if (type.isUndefined(keyIndex)) {
keyIndex = this._getKeyIndex(key);
}
if (keyIndex >= 0) {
return this._valuesForIndex[keyIndex];
}
};
/**
* Returns the original value of the specified key.
* @private
* @param {*} key - The key of the value object to be returned
* @param {number} keyIndex - The index of the key
* @returns {*} Original value
*/
Map.prototype._getOriginValue = function(key, keyIndex) {
return this._getValueObject(key, keyIndex).origin;
};
/**
* Returns key-value pair of the specified key.
* @private
* @param {*} key - The key of the value object to be returned
* @param {number} keyIndex - The index of the key
* @returns {Array} Key-value Pair
*/
Map.prototype._getKeyValuePair = function(key, keyIndex) {
return [this._getOriginKey(key), this._getOriginValue(key, keyIndex)];
};
/**
* Creates the wrapper object of original value that contains a key index
* and returns it.
* @private
* @param {type} origin - Original value
* @param {type} keyIndex - Index of the key
* @returns {{keyIndex: number, origin: *}} Value object
*/
Map.prototype._createValueObject = function(origin, keyIndex) {
return {
keyIndex: keyIndex,
origin: origin
};
};
/**
* Sets the value for the key in the Map object.
* @param {*} key - The key of the element to add to the Map object
* @param {*} value - The value of the element to add to the Map object
* @returns {Map} The Map object
*/
Map.prototype.set = function(key, value) {
var uniqueKey = this._getUniqueKey(key);
var keyIndex = this._getKeyIndex(uniqueKey);
var valueObject;
if (keyIndex < 0) {
keyIndex = this._keys.push(uniqueKey) - 1;
this.size += 1;
}
valueObject = this._createValueObject(value, keyIndex);
if (type.isString(key)) {
this._valuesForString[key] = valueObject;
} else {
this._valuesForIndex[keyIndex] = valueObject;
}
return this;
};
/**
* Returns the value associated to the key, or undefined if there is none.
* @param {*} key - The key of the element to return
* @returns {*} Element associated with the specified key
*/
Map.prototype.get = function(key) {
var uniqueKey = this._getUniqueKey(key);
var value = this._getValueObject(uniqueKey);
return value && value.origin;
};
/**
* Returns a new Iterator object that contains the keys for each element
* in the Map object in insertion order.
* @returns {Iterator} A new Iterator object
*/
Map.prototype.keys = function() {
return new MapIterator(this._keys, func.bind(this._getOriginKey, this));
};
/**
* Returns a new Iterator object that contains the values for each element
* in the Map object in insertion order.
* @returns {Iterator} A new Iterator object
*/
Map.prototype.values = function() {
return new MapIterator(this._keys, func.bind(this._getOriginValue, this));
};
/**
* Returns a new Iterator object that contains the [key, value] pairs
* for each element in the Map object in insertion order.
* @returns {Iterator} A new Iterator object
*/
Map.prototype.entries = function() {
return new MapIterator(this._keys, func.bind(this._getKeyValuePair, this));
};
/**
* Returns a boolean asserting whether a value has been associated to the key
* in the Map object or not.
* @param {*} key - The key of the element to test for presence
* @returns {boolean} True if an element with the specified key exists;
* Otherwise false
*/
Map.prototype.has = function(key) {
return !!this._getValueObject(key);
};
/**
* Removes the specified element from a Map object.
* @param {*} key - The key of the element to remove
* @function delete
* @memberof tui.util.Map.prototype
*/
// cannot use reserved keyword as a property name in IE8 and under.
Map.prototype['delete'] = function(key) {
var keyIndex;
if (type.isString(key)) {
if (this._valuesForString[key]) {
keyIndex = this._valuesForString[key].keyIndex;
delete this._valuesForString[key];
}
} else {
keyIndex = this._getKeyIndex(key);
if (keyIndex >= 0) {
delete this._valuesForIndex[keyIndex];
}
}
if (keyIndex >= 0) {
delete this._keys[keyIndex];
this.size -= 1;
}
};
/**
* Executes a provided function once per each key/value pair in the Map object,
* in insertion order.
* @param {function} callback - Function to execute for each element
* @param {thisArg} thisArg - Value to use as this when executing callback
*/
Map.prototype.forEach = function(callback, thisArg) {
thisArg = thisArg || this;
collection.forEachArray(this._keys, function(key) {
if (!type.isUndefined(key)) {
callback.call(thisArg, this._getValueObject(key).origin, key, this);
}
}, this);
};
/**
* Removes all elements from a Map object.
*/
Map.prototype.clear = function() {
Map.call(this);
};
/* eslint-enable no-extend-native */
// Use native Map object if exists.
// But only latest versions of Chrome and Firefox support full implementation.
(function() {
if (window.Map && (
(browser.firefox && browser.version >= 37) ||
(browser.chrome && browser.version >= 42)
)
) {
Map = window.Map; // eslint-disable-line no-func-assign
}
})();
module.exports = Map;