Skip to content

Commit 82e99bf

Browse files
committed
All now using SafeRange to cut down on duplicated code
1 parent 9fb1a72 commit 82e99bf

10 files changed

Lines changed: 155 additions & 96 deletions

File tree

src/utils/array/CountAllMatching.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var SafeRange = require('./SafeRange');
8+
79
/**
810
* Returns the total number of elements in the array which have a property matching the given value.
911
*
@@ -13,20 +15,28 @@
1315
* @param {array} array - The array to search.
1416
* @param {string} property - The property to test on each array element.
1517
* @param {*} value - The value to test the property against. Must pass a strict (`===`) comparison check.
18+
* @param {integer} [startIndex] - An optional start index to search from.
19+
* @param {integer} [endIndex] - An optional end index to search to.
1620
*
1721
* @return {integer} The total number of elements with properties matching the given value.
1822
*/
19-
var CountAllMatching = function (array, property, value)
23+
var CountAllMatching = function (array, property, value, startIndex, endIndex)
2024
{
25+
if (startIndex === undefined) { startIndex = 0; }
26+
if (endIndex === undefined) { endIndex = array.length; }
27+
2128
var total = 0;
2229

23-
for (var i = 0; i < array.length; i++)
30+
if (SafeRange(array, startIndex, endIndex))
2431
{
25-
var child = array[i];
26-
27-
if (child[property] === value)
32+
for (var i = startIndex; i < endIndex; i++)
2833
{
29-
total++;
34+
var child = array[i];
35+
36+
if (child[property] === value)
37+
{
38+
total++;
39+
}
3040
}
3141
}
3242

src/utils/array/EachInRange.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var SafeRange = require('./SafeRange');
8+
79
/**
810
* Passes each element in the array, between the start and end indexes, to the given callback.
911
*
@@ -21,34 +23,25 @@
2123
*/
2224
var EachInRange = function (array, callback, context, startIndex, endIndex)
2325
{
24-
var len = array.length;
25-
2626
if (startIndex === undefined) { startIndex = 0; }
27-
if (endIndex === undefined) { endIndex = len; }
28-
29-
if (endIndex > len)
30-
{
31-
endIndex = len;
32-
}
27+
if (endIndex === undefined) { endIndex = array.length; }
3328

34-
if (startIndex < 0 || startIndex > len || startIndex >= endIndex)
29+
if (SafeRange(array, startIndex, endIndex))
3530
{
36-
throw new Error('Range Error: Values outside acceptable range');
37-
}
31+
var i;
32+
var args = [ null ];
3833

39-
var i;
40-
var args = [ null ];
34+
for (i = 5; i < arguments.length; i++)
35+
{
36+
args.push(arguments[i]);
37+
}
4138

42-
for (i = 5; i < arguments.length; i++)
43-
{
44-
args.push(arguments[i]);
45-
}
46-
47-
for (i = startIndex; i < endIndex; i++)
48-
{
49-
args[0] = array[i];
39+
for (i = startIndex; i < endIndex; i++)
40+
{
41+
args[0] = array[i];
5042

51-
callback.apply(context, args);
43+
callback.apply(context, args);
44+
}
5245
}
5346

5447
return array;

src/utils/array/GetAll.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var SafeRange = require('./SafeRange');
8+
79
/**
810
* Returns all elements in the array.
911
*
1012
* You can optionally specify a matching criteria using the `property` and `value` arguments.
1113
*
12-
* For example: `getAll('visible', true)` would return only children that have their visible property set.
14+
* For example: `getAll('visible', true)` would return only elements that have their visible property set.
1315
*
14-
* Optionally you can specify a start and end index. For example if this List had 100 children,
16+
* Optionally you can specify a start and end index. For example if the array had 100 elements,
1517
* and you set `startIndex` to 0 and `endIndex` to 50, it would return matches from only
16-
* the first 50 children in the List.
18+
* the first 50 elements.
1719
*
1820
* @function Phaser.Utils.Array.GetAll
1921
* @since 3.4.0
@@ -33,15 +35,18 @@ var GetAll = function (array, property, value, startIndex, endIndex)
3335

3436
var output = [];
3537

36-
for (var i = startIndex; i < endIndex; i++)
38+
if (SafeRange(array, startIndex, endIndex))
3739
{
38-
var child = array[i];
39-
40-
if (!property ||
41-
(property && value === undefined && child.hasOwnProperty(property)) ||
42-
(property && value !== undefined && child[property] === value))
40+
for (var i = startIndex; i < endIndex; i++)
4341
{
44-
output.push(child);
42+
var child = array[i];
43+
44+
if (!property ||
45+
(property && value === undefined && child.hasOwnProperty(property)) ||
46+
(property && value !== undefined && child[property] === value))
47+
{
48+
output.push(child);
49+
}
4550
}
4651
}
4752

src/utils/array/GetFirst.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,50 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var SafeRange = require('./SafeRange');
8+
79
/**
8-
* Returns the first element from the array which optionally has a property matching the given value.
10+
* Returns the first element in the array.
11+
*
12+
* You can optionally specify a matching criteria using the `property` and `value` arguments.
13+
*
14+
* For example: `getAll('visible', true)` would return the first element that had its `visible` property set.
915
*
10-
* @function Phaser.Utils.Array.GetFirstElement
16+
* Optionally you can specify a start and end index. For example if the array had 100 elements,
17+
* and you set `startIndex` to 0 and `endIndex` to 50, it would search only the first 50 elements.
18+
*
19+
* @function Phaser.Utils.Array.GetFirst
1120
* @since 3.4.0
1221
*
1322
* @param {array} array - The array to search.
14-
* @param {string} property - The property to test on each array element.
15-
* @param {*} value - The value to test the property against. Must pass a strict (`===`) comparison check.
23+
* @param {string} [property] - The property to test on each array element.
24+
* @param {*} [value] - The value to test the property against. Must pass a strict (`===`) comparison check.
1625
* @param {integer} [startIndex=0] - An optional start index to search from.
1726
* @param {integer} [endIndex=array.length] - An optional end index to search up to (but not included)
1827
*
1928
* @return {object} The first matching element from the array, or `null` if no element could be found in the range given.
2029
*/
21-
var GetFirstElement = function (array, property, value, startIndex, endIndex)
30+
var GetFirst = function (array, property, value, startIndex, endIndex)
2231
{
2332
if (startIndex === undefined) { startIndex = 0; }
2433
if (endIndex === undefined) { endIndex = array.length; }
2534

26-
for (var i = startIndex; i < endIndex; i++)
35+
if (SafeRange(array, startIndex, endIndex))
2736
{
28-
var child = array[i];
29-
30-
if (!property || child[property] === value)
37+
for (var i = startIndex; i < endIndex; i++)
3138
{
32-
return child;
39+
var child = array[i];
40+
41+
if (!property ||
42+
(property && value === undefined && child.hasOwnProperty(property)) ||
43+
(property && value !== undefined && child[property] === value))
44+
{
45+
return child;
46+
}
3347
}
3448
}
3549

3650
return null;
3751
};
3852

39-
module.exports = GetFirstElement;
53+
module.exports = GetFirst;

src/utils/array/GetRandom.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Returns a Random element from the array.
99
*
10-
* @function Phaser.Utils.Array.GetRandomElement
10+
* @function Phaser.Utils.Array.GetRandom
1111
* @since 3.0.0
1212
*
1313
* @param {array} array - The array to select the random entry from.
@@ -16,7 +16,7 @@
1616
*
1717
* @return {object} A random element from the array, or `null` if no element could be found in the range given.
1818
*/
19-
var GetRandomElement = function (array, startIndex, length)
19+
var GetRandom = function (array, startIndex, length)
2020
{
2121
if (startIndex === undefined) { startIndex = 0; }
2222
if (length === undefined) { length = array.length; }
@@ -26,4 +26,4 @@ var GetRandomElement = function (array, startIndex, length)
2626
return (array[randomIndex] === undefined) ? null : array[randomIndex];
2727
};
2828

29-
module.exports = GetRandomElement;
29+
module.exports = GetRandom;

src/utils/array/RemoveBetween.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var SafeRange = require('./SafeRange');
8+
79
/**
810
* Removes the item within the given range in the array.
911
*
@@ -24,38 +26,32 @@
2426
*/
2527
var RemoveBetween = function (array, startIndex, endIndex, callback, context)
2628
{
27-
if (context === undefined) { context = array; }
28-
29-
var len = array.length;
30-
3129
if (startIndex === undefined) { startIndex = 0; }
32-
if (endIndex === undefined) { endIndex = len; }
30+
if (endIndex === undefined) { endIndex = array.length; }
31+
if (context === undefined) { context = array; }
3332

34-
if (endIndex > len)
33+
if (SafeRange(array, startIndex, endIndex))
3534
{
36-
endIndex = len;
37-
}
35+
var size = endIndex - startIndex;
3836

39-
if (startIndex < 0 || startIndex > len || startIndex >= endIndex)
40-
{
41-
throw new Error('Range Error: Values outside acceptable range');
42-
}
37+
var removed = array.splice(startIndex, size);
4338

44-
var size = endIndex - startIndex;
45-
46-
var removed = array.splice(startIndex, size);
47-
48-
if (callback)
49-
{
50-
for (var i = 0; i < removed.length; i++)
39+
if (callback)
5140
{
52-
var entry = removed[i];
41+
for (var i = 0; i < removed.length; i++)
42+
{
43+
var entry = removed[i];
5344

54-
callback.call(context, entry);
45+
callback.call(context, entry);
46+
}
5547
}
56-
}
5748

58-
return removed;
49+
return removed;
50+
}
51+
else
52+
{
53+
return [];
54+
}
5955
};
6056

6157
module.exports = RemoveBetween;

src/utils/array/Replace.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @param {*} oldChild - The element in the array that will be replaced.
1616
* @param {*} newChild - The element to be inserted into the array at the position of `oldChild`.
1717
*
18-
* @return {*} Returns the oldChild that was replaced.
18+
* @return {boolean} Returns true if the oldChild was successfully replaced, otherwise returns false.
1919
*/
2020
var Replace = function (array, oldChild, newChild)
2121
{
@@ -25,9 +25,13 @@ var Replace = function (array, oldChild, newChild)
2525
if (index1 !== -1 && index2 === -1)
2626
{
2727
array[index1] = newChild;
28-
}
2928

30-
return oldChild;
29+
return true;
30+
}
31+
else
32+
{
33+
return false;
34+
}
3135
};
3236

3337
module.exports = Replace;

src/utils/array/SafeRange.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* Tests if the start and end indexes are a safe range for the given array.
9+
*
10+
* @function Phaser.Utils.Array.SafeRange
11+
* @since 3.4.0
12+
*
13+
* @param {array} array - The array to check.
14+
* @param {integer} startIndex - The start index.
15+
* @param {integer} endIndex - The end index.
16+
* @param {boolean} [throwError=true] - Throw an error if the range is out of bounds.
17+
*
18+
* @return {boolean} True if the range is safe, otherwise false.
19+
*/
20+
var SafeRange = function (array, startIndex, endIndex, throwError)
21+
{
22+
var len = array.length;
23+
24+
if (startIndex < 0 ||
25+
startIndex > len ||
26+
startIndex >= endIndex ||
27+
endIndex > len ||
28+
startIndex + endIndex > len)
29+
{
30+
if (throwError)
31+
{
32+
throw new Error('Range Error: Values outside acceptable range');
33+
}
34+
35+
return false;
36+
}
37+
else
38+
{
39+
return true;
40+
}
41+
};
42+
43+
module.exports = SafeRange;

0 commit comments

Comments
 (0)