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-03-19 21:57:46 +00:00
|
|
|
/**
|
|
|
|
* @callback EachMapCallback
|
2018-03-30 12:43:58 +00:00
|
|
|
* @generic E - [entry]
|
2018-03-19 21:57:46 +00:00
|
|
|
*
|
|
|
|
* @param {string} key - [description]
|
2018-04-03 14:24:48 +00:00
|
|
|
* @param {*} entry - [description]
|
2018-03-19 21:57:46 +00:00
|
|
|
*
|
|
|
|
* @return {?boolean} [description]
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @generic K
|
|
|
|
* @generic V
|
|
|
|
* @genericUse {V[]} - [elements]
|
2018-03-27 13:59:49 +00:00
|
|
|
*
|
2018-03-23 15:54:12 +00:00
|
|
|
* @param {Array.<*>} elements - [description]
|
2018-02-09 04:35:23 +00:00
|
|
|
*/
|
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]
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {Object.<string, V>} - [$type]
|
2018-03-27 13:59:49 +00:00
|
|
|
*
|
2018-02-09 04:35:23 +00:00
|
|
|
* @name Phaser.Structs.Map#entries
|
2018-03-23 15:54:12 +00:00
|
|
|
* @type {Object.<string, *>}
|
2018-02-09 04:35:23 +00:00
|
|
|
* @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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {K} - [key]
|
|
|
|
* @genericUse {V} - [value]
|
|
|
|
* @genericUse {Phaser.Structs.Map.<K, V>} - [$return]
|
2018-03-27 13:59:49 +00:00
|
|
|
*
|
2018-02-09 04:35:23 +00:00
|
|
|
* @param {string} key - [description]
|
2018-03-20 16:15:49 +00:00
|
|
|
* @param {*} value - [description]
|
2018-02-09 04:35:23 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {K} - [key]
|
|
|
|
* @genericUse {V} - [$return]
|
2018-03-27 13:59:49 +00:00
|
|
|
*
|
2018-02-09 04:35:23 +00:00
|
|
|
* @param {string} key - [description]
|
|
|
|
*
|
2018-03-20 16:15:49 +00:00
|
|
|
* @return {*} [description]
|
2018-02-09 04:35:23 +00:00
|
|
|
*/
|
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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {V[]} - [$return]
|
2018-03-27 13:59:49 +00:00
|
|
|
*
|
2018-03-23 15:54:12 +00:00
|
|
|
* @return {Array.<*>} [description]
|
2018-02-09 04:35:23 +00:00
|
|
|
*/
|
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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {K} - [key]
|
|
|
|
*
|
2018-02-09 04:35:23 +00:00
|
|
|
* @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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {K} - [key]
|
|
|
|
* @genericUse {Phaser.Structs.Map.<K, V>} - [$return]
|
|
|
|
*
|
2018-02-09 04:35:23 +00:00
|
|
|
* @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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {Phaser.Structs.Map.<K, V>} - [$return]
|
|
|
|
*
|
2018-02-09 04:35:23 +00:00
|
|
|
* @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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {K[]} - [$return]
|
|
|
|
*
|
2018-03-19 01:00:21 +00:00
|
|
|
* @return {string[]} [description]
|
2018-02-09 04:35:23 +00:00
|
|
|
*/
|
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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {V[]} - [$return]
|
2018-03-27 13:59:49 +00:00
|
|
|
*
|
2018-03-23 15:54:12 +00:00
|
|
|
* @return {Array.<*>} [description]
|
2018-02-09 04:35:23 +00:00
|
|
|
*/
|
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;
|
|
|
|
|
2018-02-16 18:44:07 +00:00
|
|
|
// eslint-disable-next-line no-console
|
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
|
|
|
}
|
|
|
|
|
2018-02-16 18:44:07 +00:00
|
|
|
// eslint-disable-next-line no-console
|
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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {EachMapCallback.<V>} - [callback]
|
|
|
|
* @genericUse {Phaser.Structs.Map.<K, V>} - [$return]
|
2018-03-29 10:56:47 +00:00
|
|
|
*
|
|
|
|
* @param {EachMapCallback} callback - [description]
|
2018-02-09 04:35:23 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {V} - [value]
|
2018-03-27 13:59:49 +00:00
|
|
|
*
|
2018-03-20 16:15:49 +00:00
|
|
|
* @param {*} value - [description]
|
2018-02-09 04:35:23 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
2018-03-30 12:43:58 +00:00
|
|
|
* @genericUse {Phaser.Structs.Map.<K, V>} - [map,$return]
|
|
|
|
*
|
2018-02-09 04:35:23 +00:00
|
|
|
* @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;
|