From 7d022067df29333f1b87c285fb00858fd023018c Mon Sep 17 00:00:00 2001 From: photonstorm Date: Thu, 8 Dec 2016 16:41:11 +0000 Subject: [PATCH] Added newer function from v3 merge. --- v3/src/utils/array/NumberArray.js | 40 ++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/v3/src/utils/array/NumberArray.js b/v3/src/utils/array/NumberArray.js index 9ce570036..8091fa3f7 100644 --- a/v3/src/utils/array/NumberArray.js +++ b/v3/src/utils/array/NumberArray.js @@ -1,24 +1,46 @@ /** -* Create an array representing the inclusive range of numbers (usually integers) in `[start, end]`. +* Create an array representing the range of numbers (usually integers), between, and inclusive of, +* the given `start` and `end` arguments. For example: +* +* `var array = numberArray(2, 4); // array = [2, 3, 4]` +* `var array = numberArray(0, 9); // array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]` +* * This is equivalent to `numberArrayStep(start, end, 1)`. +* +* You can optionally provide a prefix and / or suffix string. If given the array will contain +* strings, not integers. For example: +* +* `var array = numberArray(1, 4, 'Level '); // array = ["Level 1", "Level 2", "Level 3", "Level 4"]` +* `var array = numberArray(5, 7, 'HD-', '.png'); // array = ["HD-5.png", "HD-6.png", "HD-7.png"]` * * @method Phaser.ArrayUtils#numberArray * @param {number} start - The minimum value the array starts with. * @param {number} end - The maximum value the array contains. -* @return {number[]} The array of number values. +* @param {string} [prefix] - Optional prefix to place before the number. If provided the array will contain strings, not integers. +* @param {string} [suffix] - Optional suffix to place after the number. If provided the array will contain strings, not integers. +* @return {number[]|string[]} The array of number values, or strings if a prefix or suffix was provided. */ -var NumberArray = function (start, end) +var NumberArray = function (start, end, prefix, suffix) { - if (start > end) - { - return; - } - var result = []; for (var i = start; i <= end; i++) { - result.push(i); + if (prefix || suffix) + { + var key = (prefix) ? prefix + i.toString() : i.toString(); + + if (suffix) + { + key = key.concat(suffix); + } + + result.push(key); + } + else + { + result.push(i); + } } return result;