primitive-iterator.js
1.99 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
'use strict';
var clear = require('es5-ext/array/#/clear')
, assign = require('es5-ext/object/assign')
, setPrototypeOf = require('es5-ext/object/set-prototype-of')
, toStringTagSymbol = require('es6-symbol').toStringTag
, d = require('d')
, autoBind = require('d/auto-bind')
, Iterator = require('es6-iterator')
, kinds = require('./iterator-kinds')
, defineProperties = Object.defineProperties, keys = Object.keys
, unBind = Iterator.prototype._unBind
, PrimitiveMapIterator;
PrimitiveMapIterator = module.exports = function (map, kind) {
if (!(this instanceof PrimitiveMapIterator)) {
return new PrimitiveMapIterator(map, kind);
}
Iterator.call(this, keys(map.__mapKeysData__), map);
if (!kind || !kinds[kind]) kind = 'key+value';
defineProperties(this, {
__kind__: d('', kind),
__keysData__: d('w', map.__mapKeysData__),
__valuesData__: d('w', map.__mapValuesData__)
});
};
if (setPrototypeOf) setPrototypeOf(PrimitiveMapIterator, Iterator);
PrimitiveMapIterator.prototype = Object.create(Iterator.prototype, assign({
constructor: d(PrimitiveMapIterator),
_resolve: d(function (i) {
if (this.__kind__ === 'value') return this.__valuesData__[this.__list__[i]];
if (this.__kind__ === 'key') return this.__keysData__[this.__list__[i]];
return [this.__keysData__[this.__list__[i]],
this.__valuesData__[this.__list__[i]]];
}),
_unBind: d(function () {
this.__keysData__ = null;
this.__valuesData__ = null;
unBind.call(this);
}),
toString: d(function () { return '[object Map Iterator]'; })
}, autoBind({
_onAdd: d(function (key) { this.__list__.push(key); }),
_onDelete: d(function (key) {
var index = this.__list__.lastIndexOf(key);
if (index < this.__nextIndex__) return;
this.__list__.splice(index, 1);
}),
_onClear: d(function () {
clear.call(this.__list__);
this.__nextIndex__ = 0;
})
})));
Object.defineProperty(PrimitiveMapIterator.prototype, toStringTagSymbol,
d('c', 'Map Iterator'));