Skip to content

Commit 6c3fac5

Browse files
committed
added: Phaser.Utils.range
1 parent d5ffe1f commit 6c3fac5

1 file changed

Lines changed: 62 additions & 3 deletions

File tree

src/utils/Utils.js

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Phaser.Utils = {
2727
l = parts.length,
2828
i = 1,
2929
current = parts[0];
30-
30+
3131
while (i < l && (obj = obj[current]))
3232
{
3333
current = parts[i];
@@ -60,7 +60,7 @@ Phaser.Utils = {
6060
l = parts.length,
6161
i = 1,
6262
current = parts[0];
63-
63+
6464
while (i < l && (obj = obj[current]))
6565
{
6666
current = parts[i];
@@ -203,6 +203,65 @@ 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 = Array(length);
258+
259+
while (++index < length) {
260+
result[index] = start;
261+
start += step;
262+
}
263+
return result;
264+
},
206265

207266
/**
208267
* Javascript string pad http://www.webtoolkit.info/.
@@ -364,7 +423,7 @@ Phaser.Utils = {
364423
/**
365424
* Mixes the source object into the destination object, returning the newly modified destination object.
366425
* Based on original code by @mudcube
367-
*
426+
*
368427
* @method Phaser.Utils.mixin
369428
* @param {object} from - The object to copy (the source object).
370429
* @param {object} to - The object to copy to (the destination object).

0 commit comments

Comments
 (0)