2016-12-09 09:32:24 +00:00
|
|
|
// The keys of a Map can be arbitrary values.
|
|
|
|
|
|
|
|
/*
|
|
|
|
var map = new Map([
|
|
|
|
[ 1, 'one' ],
|
|
|
|
[ 2, 'two' ],
|
|
|
|
[ 3, 'three' ]
|
|
|
|
]);
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Map = function (elements)
|
|
|
|
{
|
|
|
|
this.entries = {};
|
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
this.size = 0;
|
|
|
|
|
2016-12-09 09:32:24 +00:00
|
|
|
if (Array.isArray(elements))
|
|
|
|
{
|
|
|
|
for (var i = 0; i < elements.length; i++)
|
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
this.set(elements[i][0], elements[i][1]);
|
2016-12-09 09:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Map.prototype = {
|
|
|
|
|
|
|
|
set: function (key, value)
|
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
if (!this.has(key))
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
|
|
|
this.entries[key] = value;
|
2017-03-22 23:44:45 +00:00
|
|
|
this.size++;
|
2016-12-09 09:32:24 +00:00
|
|
|
}
|
2016-12-13 16:12:25 +00:00
|
|
|
|
2016-12-09 09:32:24 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
get: function (key)
|
|
|
|
{
|
|
|
|
if (this.has(key))
|
|
|
|
{
|
|
|
|
return this.entries[key];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
has: function (key)
|
|
|
|
{
|
|
|
|
return (this.entries.hasOwnProperty(key));
|
|
|
|
},
|
|
|
|
|
2016-12-13 16:12:25 +00:00
|
|
|
delete: function (key)
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
if (this.has(key))
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2016-12-13 16:12:25 +00:00
|
|
|
delete this.entries[key];
|
2017-03-22 23:44:45 +00:00
|
|
|
this.size--;
|
2016-12-09 09:32:24 +00:00
|
|
|
}
|
2016-12-13 16:12:25 +00:00
|
|
|
|
|
|
|
return this;
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
clear: function ()
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
Object.keys(this.entries).forEach(function (prop)
|
|
|
|
{
|
|
|
|
delete this.entries[prop];
|
|
|
|
});
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
this.size = 0;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
keys: function ()
|
|
|
|
{
|
|
|
|
return Object.keys(this.entries);
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
values: function ()
|
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
var output = [];
|
|
|
|
var entries = this.entries;
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
for (var key in entries)
|
|
|
|
{
|
|
|
|
output.push(entries[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
dump: function ()
|
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
var entries = this.entries;
|
|
|
|
|
2016-12-09 09:32:24 +00:00
|
|
|
console.group('Map');
|
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
for (var key in entries)
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
console.log(key, entries[key]);
|
2016-12-09 09:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.groupEnd();
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
each: function (callback)
|
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
var entries = this.entries;
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
for (var key in entries)
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
if (callback(key, entries[key]) === false)
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-22 23:44:45 +00:00
|
|
|
|
|
|
|
return this;
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
contains: function (value)
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
var entries = this.entries;
|
|
|
|
|
|
|
|
for (var key in entries)
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
if (entries[key] === value)
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
return true;
|
2016-12-09 09:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
return false;
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
// Merges all new keys from the given Map into this one
|
|
|
|
// If it encounters a key that already exists it will be skipped
|
|
|
|
// unless override = true
|
|
|
|
merge: function (map, override)
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
if (override === undefined) { override = false; }
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
var local = this.entries;
|
|
|
|
var source = map.entries;
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
for (var key in source)
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
if (local.hasOwnProperty(key) && override)
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
local[key] = source[key];
|
2016-12-09 09:32:24 +00:00
|
|
|
}
|
2017-03-22 23:44:45 +00:00
|
|
|
else
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-03-22 23:44:45 +00:00
|
|
|
this.set(key, source[key]);
|
2016-12-09 09:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
return this;
|
2016-12-09 09:32:24 +00:00
|
|
|
}
|
2017-03-22 23:44:45 +00:00
|
|
|
};
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2017-06-28 16:17:31 +00:00
|
|
|
Map.prototype.constructor = Map;
|
|
|
|
|
2016-12-09 09:32:24 +00:00
|
|
|
module.exports = Map;
|