Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / utility / times.js
1 var bindCallback = require('../internal/bindCallback');
2
3 /* Native method references for those with the same name as other `lodash` methods. */
4 var nativeFloor = Math.floor,
5     nativeIsFinite = global.isFinite,
6     nativeMin = Math.min;
7
8 /** Used as references for the maximum length and index of an array. */
9 var MAX_ARRAY_LENGTH = 4294967295;
10
11 /**
12  * Invokes the iteratee function `n` times, returning an array of the results
13  * of each invocation. The `iteratee` is bound to `thisArg` and invoked with
14  * one argument; (index).
15  *
16  * @static
17  * @memberOf _
18  * @category Utility
19  * @param {number} n The number of times to invoke `iteratee`.
20  * @param {Function} [iteratee=_.identity] The function invoked per iteration.
21  * @param {*} [thisArg] The `this` binding of `iteratee`.
22  * @returns {Array} Returns the array of results.
23  * @example
24  *
25  * var diceRolls = _.times(3, _.partial(_.random, 1, 6, false));
26  * // => [3, 6, 4]
27  *
28  * _.times(3, function(n) {
29  *   mage.castSpell(n);
30  * });
31  * // => invokes `mage.castSpell(n)` three times with `n` of `0`, `1`, and `2`
32  *
33  * _.times(3, function(n) {
34  *   this.cast(n);
35  * }, mage);
36  * // => also invokes `mage.castSpell(n)` three times
37  */
38 function times(n, iteratee, thisArg) {
39   n = nativeFloor(n);
40
41   // Exit early to avoid a JSC JIT bug in Safari 8
42   // where `Array(0)` is treated as `Array(1)`.
43   if (n < 1 || !nativeIsFinite(n)) {
44     return [];
45   }
46   var index = -1,
47       result = Array(nativeMin(n, MAX_ARRAY_LENGTH));
48
49   iteratee = bindCallback(iteratee, thisArg, 1);
50   while (++index < n) {
51     if (index < MAX_ARRAY_LENGTH) {
52       result[index] = iteratee(index);
53     } else {
54       iteratee(index);
55     }
56   }
57   return result;
58 }
59
60 module.exports = times;