Skip to content

Commit 06c6b23

Browse files
committed
moved: Utils.range to Math.numberArray
1 parent 1835453 commit 06c6b23

2 files changed

Lines changed: 58 additions & 78 deletions

File tree

src/math/Math.js

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -490,24 +490,63 @@ Phaser.Math = {
490490
},
491491

492492
/**
493-
* Returns an Array containing the numbers from min to max (inclusive).
494-
*
495-
* @method Phaser.Math#numberArray
496-
* @param {number} min - The minimum value the array starts with.
497-
* @param {number} max - The maximum value the array contains.
498-
* @return {array} The array of number values.
499-
*/
500-
numberArray: function (min, max) {
501-
502-
var result = [];
503-
504-
for (var i = min; i <= max; i++)
505-
{
506-
result.push(i);
493+
* Creates an array of numbers (positive and/or negative) progressing from
494+
* `start` up to but not including `end`. If `start` is less than `stop` a
495+
* zero-length range is created unless a negative `step` is specified.
496+
*
497+
* @static
498+
* @method Phaser.Math.numberArray
499+
* @param {number} [start=0] The start of the range.
500+
* @param {number} end The end of the range.
501+
* @param {number} [step=1] The value to increment or decrement by.
502+
* @returns {Array} Returns the new array of numbers.
503+
* @example
504+
*
505+
* Phaser.Math.numberArray(4);
506+
* // => [0, 1, 2, 3]
507+
*
508+
* Phaser.Math.numberArray(1, 5);
509+
* // => [1, 2, 3, 4]
510+
*
511+
* Phaser.Math.numberArray(0, 20, 5);
512+
* // => [0, 5, 10, 15]
513+
*
514+
* Phaser.Math.numberArray(0, -4, -1);
515+
* // => [0, -1, -2, -3]
516+
*
517+
* Phaser.Math.numberArray(1, 4, 0);
518+
* // => [1, 1, 1]
519+
*
520+
* Phaser.Math.numberArray(0);
521+
* // => []
522+
*/
523+
numberArray: function(start, end, step) {
524+
start = +start || 0;
525+
526+
// enables use as a callback for functions like `_.map`
527+
var type = typeof end;
528+
if ((type == 'number' || type == 'string') && step && step[end] === start) {
529+
end = step = null;
507530
}
531+
step = step == null ? 1 : (+step || 0);
508532

533+
if (end == null) {
534+
end = start;
535+
start = 0;
536+
} else {
537+
end = +end || 0;
538+
}
539+
// use `Array(length)` so engines like Chakra and V8 avoid slower modes
540+
// http://youtu.be/XAqIpGU8ZZk#t=17m25s
541+
var index = -1,
542+
length = Phaser.Math.max(Phaser.Math.ceil((end - start) / (step || 1)), 0),
543+
result = new Array(length);
544+
545+
while (++index < length) {
546+
result[index] = start;
547+
start += step;
548+
}
509549
return result;
510-
511550
},
512551

513552
/**
@@ -947,14 +986,14 @@ Phaser.Math = {
947986
{
948987
return 1;
949988
}
950-
989+
951990
var res = value;
952-
991+
953992
while( --value )
954993
{
955994
res *= value;
956995
}
957-
996+
958997
return res;
959998
},
960999

@@ -1386,4 +1425,4 @@ Phaser.Math = {
13861425

13871426
}())
13881427

1389-
};
1428+
};

src/utils/Utils.js

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -203,65 +203,6 @@ Phaser.Utils = {
203203
return array;
204204

205205
},
206-
/**
207-
* Creates an array of numbers (positive and/or negative) progressing from
208-
* `start` up to but not including `end`. If `start` is less than `stop` a
209-
* zero-length range is created unless a negative `step` is specified.
210-
*
211-
* @static
212-
* @method Phaser.Utils.range
213-
* @param {number} [start=0] The start of the range.
214-
* @param {number} end The end of the range.
215-
* @param {number} [step=1] The value to increment or decrement by.
216-
* @returns {Array} Returns the new array of numbers.
217-
* @example
218-
*
219-
* Phaser.Utils.range(4);
220-
* // => [0, 1, 2, 3]
221-
*
222-
* Phaser.Utils.range(1, 5);
223-
* // => [1, 2, 3, 4]
224-
*
225-
* Phaser.Utils.range(0, 20, 5);
226-
* // => [0, 5, 10, 15]
227-
*
228-
* Phaser.Utils.range(0, -4, -1);
229-
* // => [0, -1, -2, -3]
230-
*
231-
* Phaser.Utils.range(1, 4, 0);
232-
* // => [1, 1, 1]
233-
*
234-
* Phaser.Utils.range(0);
235-
* // => []
236-
*/
237-
range: function(start, end, step) {
238-
start = +start || 0;
239-
240-
// enables use as a callback for functions like `_.map`
241-
var type = typeof end;
242-
if ((type == 'number' || type == 'string') && step && step[end] === start) {
243-
end = step = null;
244-
}
245-
step = step == null ? 1 : (+step || 0);
246-
247-
if (end == null) {
248-
end = start;
249-
start = 0;
250-
} else {
251-
end = +end || 0;
252-
}
253-
// use `Array(length)` so engines like Chakra and V8 avoid slower modes
254-
// http://youtu.be/XAqIpGU8ZZk#t=17m25s
255-
var index = -1,
256-
length = Phaser.Math.max(Phaser.Math.ceil((end - start) / (step || 1)), 0),
257-
result = new Array(length);
258-
259-
while (++index < length) {
260-
result[index] = start;
261-
start += step;
262-
}
263-
return result;
264-
},
265206

266207
/**
267208
* Javascript string pad http://www.webtoolkit.info/.

0 commit comments

Comments
 (0)