Skip to content

Commit 7d02206

Browse files
committed
Added newer function from v3 merge.
1 parent 71a51b5 commit 7d02206

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

v3/src/utils/array/NumberArray.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,46 @@
11
/**
2-
* Create an array representing the inclusive range of numbers (usually integers) in `[start, end]`.
2+
* Create an array representing the range of numbers (usually integers), between, and inclusive of,
3+
* the given `start` and `end` arguments. For example:
4+
*
5+
* `var array = numberArray(2, 4); // array = [2, 3, 4]`
6+
* `var array = numberArray(0, 9); // array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]`
7+
*
38
* This is equivalent to `numberArrayStep(start, end, 1)`.
9+
*
10+
* You can optionally provide a prefix and / or suffix string. If given the array will contain
11+
* strings, not integers. For example:
12+
*
13+
* `var array = numberArray(1, 4, 'Level '); // array = ["Level 1", "Level 2", "Level 3", "Level 4"]`
14+
* `var array = numberArray(5, 7, 'HD-', '.png'); // array = ["HD-5.png", "HD-6.png", "HD-7.png"]`
415
*
516
* @method Phaser.ArrayUtils#numberArray
617
* @param {number} start - The minimum value the array starts with.
718
* @param {number} end - The maximum value the array contains.
8-
* @return {number[]} The array of number values.
19+
* @param {string} [prefix] - Optional prefix to place before the number. If provided the array will contain strings, not integers.
20+
* @param {string} [suffix] - Optional suffix to place after the number. If provided the array will contain strings, not integers.
21+
* @return {number[]|string[]} The array of number values, or strings if a prefix or suffix was provided.
922
*/
10-
var NumberArray = function (start, end)
23+
var NumberArray = function (start, end, prefix, suffix)
1124
{
12-
if (start > end)
13-
{
14-
return;
15-
}
16-
1725
var result = [];
1826

1927
for (var i = start; i <= end; i++)
2028
{
21-
result.push(i);
29+
if (prefix || suffix)
30+
{
31+
var key = (prefix) ? prefix + i.toString() : i.toString();
32+
33+
if (suffix)
34+
{
35+
key = key.concat(suffix);
36+
}
37+
38+
result.push(key);
39+
}
40+
else
41+
{
42+
result.push(i);
43+
}
2244
}
2345

2446
return result;

0 commit comments

Comments
 (0)