2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-07-04 12:58:45 +00:00
|
|
|
var Class = require('../utils/Class');
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* The keys of a Map can be arbitrary values.
|
|
|
|
* var map = new Map([
|
|
|
|
* [ 1, 'one' ],
|
|
|
|
* [ 2, 'two' ],
|
|
|
|
* [ 3, 'three' ]
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* @class Map
|
|
|
|
* @memberOf Phaser.Structs
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {array} elements - [description]
|
|
|
|
*/
|
2017-07-04 12:58:45 +00:00
|
|
|
var Map = new Class({
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2017-07-04 12:58:45 +00:00
|
|
|
initialize:
|
2017-03-22 23:44:45 +00:00
|
|
|
|
2017-07-04 12:58:45 +00:00
|
|
|
function Map (elements)
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Structs.Map#entries
|
|
|
|
* @type {object}
|
|
|
|
* @default {}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:58:45 +00:00
|
|
|
this.entries = {};
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Structs.Map#size
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-04 12:58:45 +00:00
|
|
|
this.size = 0;
|
|
|
|
|
|
|
|
if (Array.isArray(elements))
|
2016-12-09 09:32:24 +00:00
|
|
|
{
|
2017-07-04 12:58:45 +00:00
|
|
|
for (var i = 0; i < elements.length; i++)
|
|
|
|
{
|
|
|
|
this.set(elements[i][0], elements[i][1]);
|
|
|
|
}
|
2016-12-09 09:32:24 +00:00
|
|
|
}
|
2017-07-04 12:58:45 +00:00
|
|
|
},
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#set
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key - [description]
|
|
|
|
* @param {any} value - [description]
|
|
|
|
*
|
|
|
|
* @return {Phaser.Structs.Map} This Map object.
|
|
|
|
*/
|
2016-12-09 09:32:24 +00:00
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#get
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key - [description]
|
|
|
|
*
|
|
|
|
* @return {any} [description]
|
|
|
|
*/
|
2017-03-22 23:44:45 +00:00
|
|
|
get: function (key)
|
|
|
|
{
|
|
|
|
if (this.has(key))
|
|
|
|
{
|
|
|
|
return this.entries[key];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#getArray
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {array} [description]
|
|
|
|
*/
|
2017-07-07 17:13:08 +00:00
|
|
|
getArray: function ()
|
|
|
|
{
|
|
|
|
var output = [];
|
|
|
|
var entries = this.entries;
|
|
|
|
|
|
|
|
for (var key in entries)
|
|
|
|
{
|
|
|
|
output.push(entries[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#has
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key - [description]
|
|
|
|
*
|
|
|
|
* @return {boolean} [description]
|
|
|
|
*/
|
2017-03-22 23:44:45 +00:00
|
|
|
has: function (key)
|
|
|
|
{
|
|
|
|
return (this.entries.hasOwnProperty(key));
|
|
|
|
},
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#delete
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key - [description]
|
|
|
|
*
|
|
|
|
* @return {Phaser.Structs.Map} This Map object.
|
|
|
|
*/
|
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
|
|
|
},
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#clear
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.Structs.Map} This Map object.
|
|
|
|
*/
|
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];
|
2018-01-31 15:42:47 +00:00
|
|
|
|
|
|
|
}, this);
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2017-03-22 23:44:45 +00:00
|
|
|
this.size = 0;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#keys
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-03-22 23:44:45 +00:00
|
|
|
keys: function ()
|
|
|
|
{
|
|
|
|
return Object.keys(this.entries);
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#values
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {array} [description]
|
|
|
|
*/
|
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
|
|
|
},
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#dump
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
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();
|
|
|
|
},
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#each
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {function} callback - [description]
|
|
|
|
*
|
|
|
|
* @return {Phaser.Structs.Map} This Map object.
|
|
|
|
*/
|
2016-12-09 09:32:24 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2018-02-09 04:35:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#contains
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {any} value - [description]
|
|
|
|
*
|
|
|
|
* @return {boolean} [description]
|
|
|
|
*/
|
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
|
|
|
},
|
|
|
|
|
2018-02-09 04:35:23 +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.
|
|
|
|
*
|
|
|
|
* @method Phaser.Structs.Map#merge
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Structs.Map} map - [description]
|
|
|
|
* @param {boolean} [override=false] - [description]
|
|
|
|
*
|
|
|
|
* @return {Phaser.Structs.Map} This Map object.
|
|
|
|
*/
|
2017-03-22 23:44:45 +00:00
|
|
|
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-07-04 12:58:45 +00:00
|
|
|
});
|
2017-06-28 16:17:31 +00:00
|
|
|
|
2016-12-09 09:32:24 +00:00
|
|
|
module.exports = Map;
|