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