/** * @fileoverview * Implements the Map object. * @author NHN. * FE Development Lab */ '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,
* which is also undefined when accessed by index.
* 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.
* This makes it easier and faster to compare an object with each keys in the array
* through no exceptional comapring for NaN. * @private * @constant */ var _KEY_FOR_NAN = {}; /** * Constructor of MapIterator
* 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.
* For using objects and primitive values as keys, this object uses array internally.
* So if the key is not a string, get(), set(), has(), delete() will operates in O(n),
* 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
* - 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 * *