Skip to content

Commit 7795f9c

Browse files
committed
Merge pull request phaserjs#1395 from pnstickne/wip-transpose-fix
ArrayUtils - minor corrections
2 parents 152b26a + fb9eab7 commit 7795f9c

3 files changed

Lines changed: 45 additions & 32 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: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ Phaser.ArrayUtils = {
1414

1515
/**
1616
* Fetch a random entry from the given array.
17-
* Will return null if random selection is missing, or array has no entries.
17+
*
18+
* Will return null if there are no array items that fall within the specified range
19+
* or if there is no item for the randomly choosen index.
1820
*
1921
* @method
2022
* @param {any[]} objects - An array of objects.
@@ -32,13 +34,15 @@ Phaser.ArrayUtils = {
3234
if (typeof length === 'undefined') { length = objects.length; }
3335

3436
var randomIndex = startIndex + Math.floor(Math.random() * length);
35-
return objects[randomIndex] || null;
37+
return objects[randomIndex] === undefined ? null : objects[randomIndex];
3638

3739
},
3840

3941
/**
4042
* Removes a random object from the given array and returns it.
41-
* 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.
4246
*
4347
* @method
4448
* @param {any[]} objects - An array of objects.
@@ -59,7 +63,7 @@ Phaser.ArrayUtils = {
5963
if (randomIndex < objects.length)
6064
{
6165
var removed = objects.splice(randomIndex, 1);
62-
return removed[0];
66+
return removed[0] === undefined ? null : removed[0];
6367
}
6468
else
6569
{
@@ -90,21 +94,24 @@ Phaser.ArrayUtils = {
9094
},
9195

9296
/**
93-
* Transposes the elements of the given Array.
97+
* Transposes the elements of the given matrix (array of arrays).
9498
*
9599
* @method
96-
* @param {array[]} array - The array to transpose.
97-
* @return {array[]} The transposed array.
100+
* @param {Array<any[]>} array - The matrix to transpose.
101+
* @return {Array<any[]>} A new transposed matrix
98102
*/
99103
transposeMatrix: function (array) {
100104

101-
var result = new Array(array[0].length);
105+
var sourceRowCount = array.length;
106+
var sourceColCount = array[0].length;
102107

103-
for (var i = 0; i < array[0].length; i++)
108+
var result = new Array(sourceColCount);
109+
110+
for (var i = 0; i < sourceColCount; i++)
104111
{
105-
result[i] = new Array(array.length - 1);
112+
result[i] = new Array(sourceRowCount);
106113

107-
for (var j = array.length - 1; j > -1; j--)
114+
for (var j = sourceRowCount - 1; j > -1; j--)
108115
{
109116
result[i][j] = array[j][i];
110117
}
@@ -115,13 +122,14 @@ Phaser.ArrayUtils = {
115122
},
116123

117124
/**
118-
* Rotates the given array.
119-
* Based on the routine from http://jsfiddle.net/MrPolywhirl/NH42z/
125+
* Rotates the given matrix (array of arrays).
126+
*
127+
* Based on the routine from {@link http://jsfiddle.net/MrPolywhirl/NH42z/}.
120128
*
121129
* @method
122-
* @param {array[]} matrix - The array to rotate.
123-
* @param {number|string} direction - The amount to rotate. Either a number: 90, -90, 270, -270, 180 or a string: 'rotateLeft', 'rotateRight' or 'rotate180'
124-
* @return {array[]} The rotated array
130+
* @param {Array<any[]>} matrix - The array to rotate; this matrix _may_ be altered.
131+
* @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').
132+
* @return {Array<any[]>} The rotated matrix. The source matrix should be discarded for the returned matrix.
125133
*/
126134
rotateMatrix: function (matrix, direction) {
127135

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)