f53c8c1620be4c7bc0cae9b079a9c6805c160fa2
[aai/esr-gui.git] /
1 "use strict"
2
3 // We have an ES6 Map available, return the native instance
4 if(typeof global.Map !== 'undefined') {
5   module.exports = global.Map;
6   module.exports.Map = global.Map;
7 } else {
8   // We will return a polyfill
9   var Map = function(array) {
10     this._keys = [];
11     this._values = {};
12
13     for(var i = 0; i < array.length; i++) {
14       if(array[i] == null) continue;  // skip null and undefined
15       var entry = array[i];
16       var key = entry[0];
17       var value = entry[1];
18       // Add the key to the list of keys in order
19       this._keys.push(key);
20       // Add the key and value to the values dictionary with a point
21       // to the location in the ordered keys list
22       this._values[key] = {v: value, i: this._keys.length - 1};
23     }
24   }
25
26   Map.prototype.clear = function() {
27     this._keys = [];
28     this._values = {};
29   }
30
31   Map.prototype.delete = function(key) {
32     var value = this._values[key];
33     if(value == null) return false;
34     // Delete entry
35     delete this._values[key];
36     // Remove the key from the ordered keys list
37     this._keys.splice(value.i, 1);
38     return true;
39   }
40
41   Map.prototype.entries = function() {
42     var self = this;
43     var index = 0;
44
45     return {
46       next: function() {
47         var key = self._keys[index++];
48         return {
49           value: key !== undefined ? [key, self._values[key].v] : undefined,
50           done: key !== undefined ? false : true
51         }
52       }
53     };
54   }
55
56   Map.prototype.forEach = function(callback, self) {
57     self = self || this;
58
59     for(var i = 0; i < this._keys.length; i++) {
60       var key = this._keys[i];
61       // Call the forEach callback
62       callback.call(self, this._values[key].v, key, self);
63     }
64   }
65
66   Map.prototype.get = function(key) {
67     return this._values[key] ? this._values[key].v : undefined;
68   }
69
70   Map.prototype.has = function(key) {
71     return this._values[key] != null;
72   }
73
74   Map.prototype.keys = function(key) {
75     var self = this;
76     var index = 0;
77
78     return {
79       next: function() {
80         var key = self._keys[index++];
81         return {
82           value: key !== undefined ? key : undefined,
83           done: key !== undefined ? false : true
84         }
85       }
86     };
87   }
88
89   Map.prototype.set = function(key, value) {
90     if(this._values[key]) {
91       this._values[key].v = value;
92       return this;
93     }
94
95     // Add the key to the list of keys in order
96     this._keys.push(key);
97     // Add the key and value to the values dictionary with a point
98     // to the location in the ordered keys list
99     this._values[key] = {v: value, i: this._keys.length - 1};
100     return this;
101   }
102
103   Map.prototype.values = function(key, value) {
104     var self = this;
105     var index = 0;
106
107     return {
108       next: function() {
109         var key = self._keys[index++];
110         return {
111           value: key !== undefined ? self._values[key].v : undefined,
112           done: key !== undefined ? false : true
113         }
114       }
115     };
116   }
117
118   // Last ismaster
119   Object.defineProperty(Map.prototype, 'size', {
120     enumerable:true,
121     get: function() { return this._keys.length; }
122   });
123
124   module.exports = Map;
125   module.exports.Map = Map;
126 }