/
GetAll.js
56 lines (49 loc) · 1.82 KB
/
GetAll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var SafeRange = require('./SafeRange');
/**
* Returns all elements in the array.
*
* You can optionally specify a matching criteria using the `property` and `value` arguments.
*
* For example: `getAll('visible', true)` would return only elements that have their visible property set.
*
* Optionally you can specify a start and end index. For example if the array had 100 elements,
* and you set `startIndex` to 0 and `endIndex` to 50, it would return matches from only
* the first 50 elements.
*
* @function Phaser.Utils.Array.GetAll
* @since 3.4.0
*
* @param {array} array - The array to search.
* @param {string} [property] - The property to test on each array element.
* @param {*} [value] - The value to test the property against. Must pass a strict (`===`) comparison check.
* @param {number} [startIndex] - An optional start index to search from.
* @param {number} [endIndex] - An optional end index to search to.
*
* @return {array} All matching elements from the array.
*/
var GetAll = function (array, property, value, startIndex, endIndex)
{
if (startIndex === undefined) { startIndex = 0; }
if (endIndex === undefined) { endIndex = array.length; }
var output = [];
if (SafeRange(array, startIndex, endIndex))
{
for (var i = startIndex; i < endIndex; i++)
{
var child = array[i];
if (!property ||
(property && value === undefined && child.hasOwnProperty(property)) ||
(property && value !== undefined && child[property] === value))
{
output.push(child);
}
}
}
return output;
};
module.exports = GetAll;