Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var isIterateeCall = require('../internal/isIterateeCall');
2
3
/** Native method references. */
4
var ceil = Math.ceil;
5
6
/* Native method references for those with the same name as other `lodash` methods. */
7
var nativeMax = Math.max;
8
9
/**
10
* Creates an array of numbers (positive and/or negative) progressing from
11
* `start` up to, but not including, `end`. If `end` is not specified it is
12
* set to `start` with `start` then set to `0`. If `end` is less than `start`
13
* a zero-length range is created unless a negative `step` is specified.
14
*
15
* @static
16
* @memberOf _
17
* @category Utility
18
* @param {number} [start=0] The start of the range.
19
* @param {number} end The end of the range.
20
* @param {number} [step=1] The value to increment or decrement by.
21
* @returns {Array} Returns the new array of numbers.
22
* @example
23
*
24
* _.range(4);
25
* // => [0, 1, 2, 3]
26
*
27
* _.range(1, 5);
28
* // => [1, 2, 3, 4]
29
*
30
* _.range(0, 20, 5);
31
* // => [0, 5, 10, 15]
32
*
33
* _.range(0, -4, -1);
34
* // => [0, -1, -2, -3]
35
*
36
* _.range(1, 4, 0);
37
* // => [1, 1, 1]
38
*
39
* _.range(0);
40
* // => []
41
*/
42
function range(start, end, step) {
43
if (step && isIterateeCall(start, end, step)) {
44
end = step = null;
45
}
46
start = +start || 0;
47
step = step == null ? 1 : (+step || 0);
48
49
if (end == null) {
50
end = start;
51
start = 0;
52
} else {
53
end = +end || 0;
54
}
55
// Use `Array(length)` so engines like Chakra and V8 avoid slower modes.
56
// See https://youtu.be/XAqIpGU8ZZk#t=17m25s for more details.
57
var index = -1,
58
length = nativeMax(ceil((end - start) / (step || 1)), 0),
59
result = Array(length);
60
61
while (++index < length) {
62
result[index] = start;
63
start += step;
64
}
65
return result;
66
}
67
68
module.exports = range;
69
70