Skip to content

Commit fb9eab7

Browse files
committed
Utils - cleanup
- Updated linked documentation - Also updated `removeRandomItem` per the contract and for consistency with `getRandomItem`.
1 parent bdacef8 commit fb9eab7

3 files changed

Lines changed: 25 additions & 18 deletions

File tree

src/math/Math.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -930,13 +930,15 @@ Phaser.Math = {
930930

931931
/**
932932
* Fetch a random entry from the given array.
933-
* Will return null if random selection is missing, or array has no entries.
933+
*
934+
* Will return null if there are no array items that fall within the specified range
935+
* or if there is no item for the randomly choosen index.
934936
*
935937
* @method Phaser.Math#getRandom
936938
* @param {any[]} objects - An array of objects.
937-
* @param {number} startIndex - Optional offset off the front of the array. Default value is 0, or the beginning of the array.
938-
* @param {number} length - Optional restriction on the number of values you want to randomly select from.
939-
* @return {any} The random object that was selected.
939+
* @param {integer} startIndex - Optional offset off the front of the array. Default value is 0, or the beginning of the array.
940+
* @param {integer} length - Optional restriction on the number of values you want to randomly select from.
941+
* @return {object} The random object that was selected.
940942
* @deprecated 2.2.0 - Use {@link Phaser.ArrayUtils.getRandomItem}
941943
*/
942944
getRandom: function (objects, startIndex, length) {
@@ -945,13 +947,15 @@ Phaser.Math = {
945947

946948
/**
947949
* Removes a random object from the given array and returns it.
948-
* Will return null if random selection is missing, or array has no entries.
950+
*
951+
* Will return null if there are no array items that fall within the specified range
952+
* or if there is no item for the randomly choosen index.
949953
*
950954
* @method Phaser.Math#removeRandom
951955
* @param {any[]} objects - An array of objects.
952-
* @param {number} startIndex - Optional offset off the front of the array. Default value is 0, or the beginning of the array.
953-
* @param {number} length - Optional restriction on the number of values you want to randomly select from.
954-
* @return {any} The random object that was removed.
956+
* @param {integer} startIndex - Optional offset off the front of the array. Default value is 0, or the beginning of the array.
957+
* @param {integer} length - Optional restriction on the number of values you want to randomly select from.
958+
* @return {object} The random object that was removed.
955959
* @deprecated 2.2.0 - Use {@link Phaser.ArrayUtils.removeRandomItem}
956960
*/
957961
removeRandom: function (objects, startIndex, length) {

src/utils/ArrayUtils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ Phaser.ArrayUtils = {
4040

4141
/**
4242
* Removes a random object from the given array and returns it.
43-
* Will return null if random selection is missing, or array has no entries.
43+
*
44+
* Will return null if there are no array items that fall within the specified range
45+
* or if there is no item for the randomly choosen index.
4446
*
4547
* @method
4648
* @param {any[]} objects - An array of objects.
@@ -61,7 +63,7 @@ Phaser.ArrayUtils = {
6163
if (randomIndex < objects.length)
6264
{
6365
var removed = objects.splice(randomIndex, 1);
64-
return removed[0];
66+
return removed[0] === undefined ? null : removed[0];
6567
}
6668
else
6769
{

src/utils/Utils.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,26 @@ Phaser.Utils = {
102102
},
103103

104104
/**
105-
* Transposes the elements of the given Array.
105+
* Transposes the elements of the given matrix (array of arrays).
106106
*
107107
* @method Phaser.Utils.transposeArray
108-
* @param {array[]} array - The array to transpose.
109-
* @return {array[]} The transposed array.
108+
* @param {Array<any[]>} array - The matrix to transpose.
109+
* @return {Array<any[]>} A new transposed matrix
110110
* @deprecated 2.2.0 - Use Phaser.ArrayUtils.transposeMatrix
111111
*/
112112
transposeArray: function (array) {
113113
return Phaser.ArrayUtils.transposeMatrix(array);
114114
},
115115

116116
/**
117-
* Rotates the given array.
118-
* Based on the routine from http://jsfiddle.net/MrPolywhirl/NH42z/
117+
* Rotates the given matrix (array of arrays).
118+
*
119+
* Based on the routine from {@link http://jsfiddle.net/MrPolywhirl/NH42z/}.
119120
*
120121
* @method Phaser.Utils.rotateArray
121-
* @param {array[]} matrix - The array to rotate.
122-
* @param {number|string} direction - The amount to rotate. Either a number: 90, -90, 270, -270, 180 or a string: 'rotateLeft', 'rotateRight' or 'rotate180'
123-
* @return {array[]} The rotated array
122+
* @param {Array<any[]>} matrix - The array to rotate; this matrix _may_ be altered.
123+
* @param {number|string} direction - The amount to rotate: the roation in degrees (90, -90, 270, -270, 180) or a string command ('rotateLeft', 'rotateRight' or 'rotate180').
124+
* @return {Array<any[]>} The rotated matrix. The source matrix should be discarded for the returned matrix.
124125
* @deprecated 2.2.0 - Use Phaser.ArrayUtils.rotateMatrix
125126
*/
126127
rotateArray: function (matrix, direction) {

0 commit comments

Comments
 (0)