forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberArrayStep.js
More file actions
71 lines (63 loc) · 1.76 KB
/
NumberArrayStep.js
File metadata and controls
71 lines (63 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var RoundAwayFromZero = require('../../math/RoundAwayFromZero');
/**
* Create an array of numbers (positive and/or negative) progressing from `start`
* up to but not including `end` by advancing by `step`.
*
* If `start` is less than `end` a zero-length range is created unless a negative `step` is specified.
*
* Certain values for `start` and `end` (eg. NaN/undefined/null) are currently coerced to 0;
* for forward compatibility make sure to pass in actual numbers.
*
* @example
* NumberArrayStep(4);
* // => [0, 1, 2, 3]
*
* NumberArrayStep(1, 5);
* // => [1, 2, 3, 4]
*
* NumberArrayStep(0, 20, 5);
* // => [0, 5, 10, 15]
*
* NumberArrayStep(0, -4, -1);
* // => [0, -1, -2, -3]
*
* NumberArrayStep(1, 4, 0);
* // => [1, 1, 1]
*
* NumberArrayStep(0);
* // => []
*
* @function Phaser.Utils.Array.NumberArrayStep
* @since 3.0.0
*
* @param {number} [start=0] - The start of the range.
* @param {number} [end=null] - The end of the range.
* @param {number} [step=1] - The value to increment or decrement by.
*
* @return {number[]} [description]
*/
var NumberArrayStep = function (start, end, step)
{
if (start === undefined) { start = 0; }
if (end === undefined) { end = null; }
if (step === undefined) { step = 1; }
if (end === null)
{
end = start;
start = 0;
}
var result = [];
var total = Math.max(RoundAwayFromZero((end - start) / (step || 1)), 0);
for (var i = 0; i < total; i++)
{
result.push(start);
start += step;
}
return result;
};
module.exports = NumberArrayStep;