Skip to content

Commit

Permalink
Fixed Array.Utils.GetFirst so it correctly handles a negative start…
Browse files Browse the repository at this point in the history
… index. Fixes Container.getByName returning null and various other similar methods. Fix #7040
  • Loading branch information
photonstorm committed Feb 11, 2025
1 parent de739a6 commit cf1d631
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/utils/array/GetFirst.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var SafeRange = require('./SafeRange');
*
* 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 search only the first 50 elements.
*
* You can also specify a negative `startIndex`, such as `-1`, which would start the search at the end of the array
*
* @function Phaser.Utils.Array.GetFirst
Expand All @@ -23,39 +24,51 @@ var SafeRange = require('./SafeRange');
* @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=0] - An optional start index to search from. You cn also set `startIndex` to -1 to start the search from the end of the array.
* @param {number} [startIndex=0] - An optional start index to search from. You can also set `startIndex` to -1 to start the search from the end of the array.
* @param {number} [endIndex=array.length] - An optional end index to search up to (but not included)
*
* @return {?object} The first matching element from the array, or `null` if no element could be found in the range given.
*/
var GetFirst = function (array, property, value, startIndex, endIndex)
{
if (startIndex === undefined) { startIndex = 0; }
if (endIndex === undefined) { endIndex = startIndex >= 0 ? array.length - 1 : 0; }
if (startIndex === -1) { startIndex = array.length - 1; }
if (endIndex === undefined) { endIndex = array.length; }

var i, child;

if (SafeRange(array, Math.min(startIndex, endIndex), Math.max(startIndex, endIndex)))
if (startIndex !== -1)
{
var step = startIndex < endIndex ? 1 : -1;
var count = Math.abs(endIndex - startIndex);
var index = startIndex;

for (i = 0; i < count; i += step)
if (SafeRange(array, startIndex, endIndex))
{
child = array[index];

if (!property ||
(property && value === undefined && child.hasOwnProperty(property)) ||
(property && value !== undefined && child[property] === value))
for (var i = startIndex; i < endIndex; i++)
{
return child;
var child = array[i];

if (!property ||
(property && value === undefined && child.hasOwnProperty(property)) ||
(property && value !== undefined && child[property] === value))
{
return child;
}
}
}
}
else
{
if (SafeRange(array, 0, endIndex))
{
for (var i = endIndex; i >= 0; i--)
{
var child = array[i];

if (!property ||
(property && value === undefined && child.hasOwnProperty(property)) ||
(property && value !== undefined && child[property] === value))
{
return child;
}
}

index += step;
}
}

return null;
};

Expand Down

0 comments on commit cf1d631

Please sign in to comment.