phaser/src/structs/Map.js

334 lines
6.3 KiB
JavaScript
Raw Normal View History

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}
*/
var Class = require('../utils/Class');
2018-03-19 21:57:46 +00:00
/**
* @callback EachMapCallback
2018-03-23 15:54:12 +00:00
* @template T
2018-03-19 21:57:46 +00:00
*
* @param {string} key - [description]
2018-03-23 15:54:12 +00:00
* @param {T} 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' ]
* ]);
*
2018-03-23 15:54:12 +00:00
* @generic T
*
2018-02-09 04:35:23 +00:00
* @class Map
* @memberOf Phaser.Structs
* @constructor
* @since 3.0.0
*
2018-03-23 15:54:12 +00:00
* @param {Array.<*>} elements - [description]
2018-02-09 04:35:23 +00:00
*/
var Map = new Class({
initialize:
function Map (elements)
{
2018-02-09 04:35:23 +00:00
/**
* [description]
*
* @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
*/
this.entries = {};
2018-02-09 04:35:23 +00:00
/**
* [description]
*
* @name Phaser.Structs.Map#size
* @type {number}
* @default 0
* @since 3.0.0
*/
this.size = 0;
if (Array.isArray(elements))
{
for (var i = 0; i < elements.length; i++)
{
this.set(elements[i][0], elements[i][1]);
}
}
},
2018-02-09 04:35:23 +00:00
/**
* [description]
*
* @method Phaser.Structs.Map#set
* @since 3.0.0
*
* @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.
*/
set: function (key, value)
{
if (!this.has(key))
{
this.entries[key] = value;
this.size++;
}
return this;
},
2018-02-09 04:35:23 +00:00
/**
* [description]
*
* @method Phaser.Structs.Map#get
* @since 3.0.0
*
* @param {string} key - [description]
*
2018-03-20 16:15:49 +00:00
* @return {*} [description]
2018-02-09 04:35:23 +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-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
*
* @param {string} key - [description]
*
* @return {boolean} [description]
*/
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.
*/
delete: function (key)
{
if (this.has(key))
{
delete this.entries[key];
this.size--;
}
return this;
},
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.
*/
clear: function ()
{
Object.keys(this.entries).forEach(function (prop)
{
delete this.entries[prop];
2018-01-31 15:42:47 +00:00
}, this);
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-19 01:00:21 +00:00
* @return {string[]} [description]
2018-02-09 04:35:23 +00:00
*/
keys: function ()
{
return Object.keys(this.entries);
},
2018-02-09 04:35:23 +00:00
/**
* [description]
*
* @method Phaser.Structs.Map#values
* @since 3.0.0
*
2018-03-23 15:54:12 +00:00
* @return {Array.<*>} [description]
2018-02-09 04:35:23 +00:00
*/
values: 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#dump
* @since 3.0.0
*/
dump: function ()
{
var entries = this.entries;
2018-02-16 18:44:07 +00:00
// eslint-disable-next-line no-console
console.group('Map');
for (var key in entries)
{
console.log(key, entries[key]);
}
2018-02-16 18:44:07 +00:00
// eslint-disable-next-line no-console
console.groupEnd();
},
2018-02-09 04:35:23 +00:00
/**
* [description]
*
* @method Phaser.Structs.Map#each
* @since 3.0.0
*
2018-03-23 15:54:12 +00:00
* @param {EachMapCallback.<T>} callback - [description]
2018-02-09 04:35:23 +00:00
*
* @return {Phaser.Structs.Map} This Map object.
*/
each: function (callback)
{
var entries = this.entries;
for (var key in entries)
{
if (callback(key, entries[key]) === false)
{
break;
}
}
return this;
},
2018-02-09 04:35:23 +00:00
/**
* [description]
*
* @method Phaser.Structs.Map#contains
* @since 3.0.0
*
2018-03-20 16:15:49 +00:00
* @param {*} value - [description]
2018-02-09 04:35:23 +00:00
*
* @return {boolean} [description]
*/
contains: function (value)
{
var entries = this.entries;
for (var key in entries)
{
if (entries[key] === value)
{
return true;
}
}
return false;
},
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.
*/
merge: function (map, override)
{
if (override === undefined) { override = false; }
var local = this.entries;
var source = map.entries;
for (var key in source)
{
if (local.hasOwnProperty(key) && override)
{
local[key] = source[key];
}
else
{
this.set(key, source[key]);
}
}
return this;
}
});
2017-06-28 16:17:31 +00:00
module.exports = Map;