|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var clear = require('es5-ext/array/#/clear') |
| 4 | + , setPrototypeOf = require('es5-ext/object/set-prototype-of') |
| 5 | + , callable = require('es5-ext/object/valid-callable') |
| 6 | + , validValue = require('es5-ext/object/valid-value') |
| 7 | + , d = require('d/d') |
| 8 | + , ee = require('event-emitter/lib/core') |
| 9 | + , iterator = require('es6-iterator/valid-iterable') |
| 10 | + , forOf = require('es6-iterator/for-of') |
| 11 | + , getSetData = require('es6-set/_get-set-data') |
| 12 | + , Iterator = require('./_iterator') |
| 13 | + , isNative = require('./is-native-implemented') |
| 14 | + |
| 15 | + , call = Function.prototype.call, defineProperties = Object.defineProperties |
| 16 | + , MapPoly; |
| 17 | + |
| 18 | +module.exports = MapPoly = function (/*iterable, comparator*/) { |
| 19 | + var iterable = arguments[0], comparator = arguments[1], keys, values; |
| 20 | + if (!(this instanceof MapPoly)) return new MapPoly(iterable, comparator); |
| 21 | + if (this.__mapKeysData__ !== undefined) { |
| 22 | + throw new TypeError(this + " cannot be reinitialized"); |
| 23 | + } |
| 24 | + if (iterable != null) iterator(iterable); |
| 25 | + if (comparator != null) { |
| 26 | + if (comparator !== 'is') throw new RangeError("Unknown comparator"); |
| 27 | + } |
| 28 | + defineProperties(this, { |
| 29 | + __mapKeysData__: d('', keys = getSetData(comparator)), |
| 30 | + __mapValuesData__: d('', values = []) |
| 31 | + }); |
| 32 | + if (!iterable) return; |
| 33 | + forOf(iterable, function (value) { |
| 34 | + var key = validValue(value)[0]; |
| 35 | + value = value[1]; |
| 36 | + if (keys.eIndexOf(key) !== -1) return; |
| 37 | + keys.push(key); |
| 38 | + values.push(value); |
| 39 | + }, this); |
| 40 | +}; |
| 41 | + |
| 42 | +if (isNative) { |
| 43 | + if (setPrototypeOf) setPrototypeOf(MapPoly, Map); |
| 44 | + MapPoly.prototype = Object.create(Map.prototype, { |
| 45 | + constructor: d(MapPoly) |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +ee(defineProperties(MapPoly.prototype, { |
| 50 | + clear: d(function () { |
| 51 | + if (!this.__mapKeysData__.length) return; |
| 52 | + this.__mapKeysData__.clear(); |
| 53 | + clear.call(this.__mapValuesData__); |
| 54 | + this.emit('_clear'); |
| 55 | + }), |
| 56 | + delete: d(function (key) { |
| 57 | + var index = this.__mapKeysData__.eIndexOf(key); |
| 58 | + if (index === -1) return false; |
| 59 | + this.__mapKeysData__.splice(index, 1); |
| 60 | + this.__mapValuesData__.splice(index, 1); |
| 61 | + this.emit('_delete', index, key); |
| 62 | + return true; |
| 63 | + }), |
| 64 | + entries: d(function () { return new Iterator(this, 'key+value'); }), |
| 65 | + forEach: d(function (cb/*, thisArg*/) { |
| 66 | + var thisArg = arguments[1], iterator, result; |
| 67 | + callable(cb); |
| 68 | + iterator = this.entries(); |
| 69 | + result = iterator.next(); |
| 70 | + while (!result.done) { |
| 71 | + call.call(cb, thisArg, result.value[1], result.value[0], this); |
| 72 | + result = iterator.next(); |
| 73 | + } |
| 74 | + }), |
| 75 | + get: d(function (key) { |
| 76 | + var index = this.__mapKeysData__.eIndexOf(key); |
| 77 | + if (index === -1) return; |
| 78 | + return this.__mapValuesData__[index]; |
| 79 | + }), |
| 80 | + has: d(function (key) { |
| 81 | + return (this.__mapKeysData__.eIndexOf(key) !== -1); |
| 82 | + }), |
| 83 | + keys: d(function () { return new Iterator(this, 'key'); }), |
| 84 | + set: d(function (key, value) { |
| 85 | + var index = this.__mapKeysData__.eIndexOf(key); |
| 86 | + if (index === -1) index = this.__mapKeysData__.push(key) - 1; |
| 87 | + this.__mapValuesData__[index] = value; |
| 88 | + this.emit('_add', index, key); |
| 89 | + return this; |
| 90 | + }), |
| 91 | + size: d.gs(function () { return this.__mapKeysData__.length; }), |
| 92 | + values: d(function () { return new Iterator(this, 'value'); }), |
| 93 | + '@@iterator': d(function () { return this.entries(); }), |
| 94 | + '@@toStringTag': d('c', 'Map'), |
| 95 | + toString: d(function () { return '[object Map]'; }) |
| 96 | +})); |
0 commit comments