Skip to content

Commit 591bf82

Browse files
committed
Phaser.ArrayUtils.numberArray now has optional prefix and suffix arguments, allowing you to do: numberArray(1, 4, 'Level ') and the Array will contain ["Level 1", "Level 2", "Level 3", "Level 4"].
1 parent 775dee0 commit 591bf82

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
357357
* Device.css3D has been removed, and the function that tested it is no longer run.
358358
* Device.isConsoleOpen has been removed. The function only worked on a very limited set of old browsers.
359359
* Device.littleEndian has been removed, you can use Device.LITTLE_ENDIAN instead.
360+
* Phaser.ArrayUtils.numberArray now has optional `prefix` and `suffix` arguments, allowing you to do: `numberArray(1, 4, 'Level ')` and the Array will contain `["Level 1", "Level 2", "Level 3", "Level 4"]`.
360361

361362

362363
### Bug Fixes

src/utils/ArrayUtils.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,25 +260,51 @@ Phaser.ArrayUtils = {
260260
},
261261

262262
/**
263-
* Create an array representing the inclusive range of numbers (usually integers) in `[start, end]`.
263+
* Create an array representing the range of numbers (usually integers), between, and inclusive of,
264+
* the given `start` and `end` arguments. For example:
265+
*
266+
* `var array = numberArray(2, 4); // array = [2, 3, 4]`
267+
* `var array = numberArray(0, 9); // array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]`
268+
*
264269
* This is equivalent to `numberArrayStep(start, end, 1)`.
270+
*
271+
* You can optionally provide a prefix and / or suffix string. If given the array will contain
272+
* strings, not integers. For example:
273+
*
274+
* `var array = numberArray(1, 4, 'Level '); // array = ["Level 1", "Level 2", "Level 3", "Level 4"]`
275+
* `var array = numberArray(5, 7, 'HD-', '.png'); // array = ["HD-5.png", "HD-6.png", "HD-7.png"]`
265276
*
266277
* @method Phaser.ArrayUtils#numberArray
267278
* @param {number} start - The minimum value the array starts with.
268279
* @param {number} end - The maximum value the array contains.
269-
* @return {number[]} The array of number values.
280+
* @param {string} [prefix] - Optional prefix to place before the number. If provided the array will contain strings, not integers.
281+
* @param {string} [suffix] - Optional suffix to place after the number. If provided the array will contain strings, not integers.
282+
* @return {number[]|string[]} The array of number values, or strings if a prefix or suffix was provided.
270283
*/
271-
numberArray: function (start, end) {
272-
284+
numberArray: function (start, end, prefix, suffix)
285+
{
273286
var result = [];
274287

275288
for (var i = start; i <= end; i++)
276289
{
277-
result.push(i);
290+
if (prefix || suffix)
291+
{
292+
var key = (prefix) ? prefix + i.toString() : i.toString();
293+
294+
if (suffix)
295+
{
296+
key = key.concat(suffix);
297+
}
298+
299+
result.push(key);
300+
}
301+
else
302+
{
303+
result.push(i);
304+
}
278305
}
279306

280307
return result;
281-
282308
},
283309

284310
/**

0 commit comments

Comments
 (0)