b9eada37c14455214c6a7cd642ee4f7973f985af
[platform/framework/web/crosswalk-tizen.git] /
1 'use strict';
2
3 var clear             = require('es5-ext/array/#/clear')
4   , assign            = require('es5-ext/object/assign')
5   , setPrototypeOf    = require('es5-ext/object/set-prototype-of')
6   , toStringTagSymbol = require('es6-symbol').toStringTag
7   , d                 = require('d')
8   , autoBind          = require('d/auto-bind')
9   , Iterator          = require('es6-iterator')
10   , kinds             = require('./iterator-kinds')
11
12   , defineProperties = Object.defineProperties, keys = Object.keys
13   , unBind = Iterator.prototype._unBind
14   , PrimitiveMapIterator;
15
16 PrimitiveMapIterator = module.exports = function (map, kind) {
17         if (!(this instanceof PrimitiveMapIterator)) {
18                 return new PrimitiveMapIterator(map, kind);
19         }
20         Iterator.call(this, keys(map.__mapKeysData__), map);
21         if (!kind || !kinds[kind]) kind = 'key+value';
22         defineProperties(this, {
23                 __kind__: d('', kind),
24                 __keysData__: d('w', map.__mapKeysData__),
25                 __valuesData__: d('w', map.__mapValuesData__)
26         });
27 };
28 if (setPrototypeOf) setPrototypeOf(PrimitiveMapIterator, Iterator);
29
30 PrimitiveMapIterator.prototype = Object.create(Iterator.prototype, assign({
31         constructor: d(PrimitiveMapIterator),
32         _resolve: d(function (i) {
33                 if (this.__kind__ === 'value') return this.__valuesData__[this.__list__[i]];
34                 if (this.__kind__ === 'key') return this.__keysData__[this.__list__[i]];
35                 return [this.__keysData__[this.__list__[i]],
36                         this.__valuesData__[this.__list__[i]]];
37         }),
38         _unBind: d(function () {
39                 this.__keysData__ = null;
40                 this.__valuesData__ = null;
41                 unBind.call(this);
42         }),
43         toString: d(function () { return '[object Map Iterator]'; })
44 }, autoBind({
45         _onAdd: d(function (key) { this.__list__.push(key); }),
46         _onDelete: d(function (key) {
47                 var index = this.__list__.lastIndexOf(key);
48                 if (index < this.__nextIndex__) return;
49                 this.__list__.splice(index, 1);
50         }),
51         _onClear: d(function () {
52                 clear.call(this.__list__);
53                 this.__nextIndex__ = 0;
54         })
55 })));
56 Object.defineProperty(PrimitiveMapIterator.prototype, toStringTagSymbol,
57         d('c', 'Map Iterator'));