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;
8 // We will return a polyfill
9 var Map = function(array) {
13 for(var i = 0; i < array.length; i++) {
14 if(array[i] == null) continue; // skip null and undefined
18 // Add the key to the list of keys in order
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};
26 Map.prototype.clear = function() {
31 Map.prototype.delete = function(key) {
32 var value = this._values[key];
33 if(value == null) return false;
35 delete this._values[key];
36 // Remove the key from the ordered keys list
37 this._keys.splice(value.i, 1);
41 Map.prototype.entries = function() {
47 var key = self._keys[index++];
49 value: key !== undefined ? [key, self._values[key].v] : undefined,
50 done: key !== undefined ? false : true
56 Map.prototype.forEach = function(callback, self) {
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);
66 Map.prototype.get = function(key) {
67 return this._values[key] ? this._values[key].v : undefined;
70 Map.prototype.has = function(key) {
71 return this._values[key] != null;
74 Map.prototype.keys = function(key) {
80 var key = self._keys[index++];
82 value: key !== undefined ? key : undefined,
83 done: key !== undefined ? false : true
89 Map.prototype.set = function(key, value) {
90 if(this._values[key]) {
91 this._values[key].v = value;
95 // Add the key to the list of keys in order
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};
103 Map.prototype.values = function(key, value) {
109 var key = self._keys[index++];
111 value: key !== undefined ? self._values[key].v : undefined,
112 done: key !== undefined ? false : true
119 Object.defineProperty(Map.prototype, 'size', {
121 get: function() { return this._keys.length; }
124 module.exports = Map;
125 module.exports.Map = Map;