|
1 | 1 | /** |
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 | +* |
3 | 8 | * 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"]` |
4 | 15 | * |
5 | 16 | * @method Phaser.ArrayUtils#numberArray |
6 | 17 | * @param {number} start - The minimum value the array starts with. |
7 | 18 | * @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. |
9 | 22 | */ |
10 | | -var NumberArray = function (start, end) |
| 23 | +var NumberArray = function (start, end, prefix, suffix) |
11 | 24 | { |
12 | | - if (start > end) |
13 | | - { |
14 | | - return; |
15 | | - } |
16 | | - |
17 | 25 | var result = []; |
18 | 26 |
|
19 | 27 | for (var i = start; i <= end; i++) |
20 | 28 | { |
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 | + } |
22 | 44 | } |
23 | 45 |
|
24 | 46 | return result; |
|
0 commit comments