1 var bindCallback = require('../internal/bindCallback');
3 /* Native method references for those with the same name as other `lodash` methods. */
4 var nativeFloor = Math.floor,
5 nativeIsFinite = global.isFinite,
8 /** Used as references for the maximum length and index of an array. */
9 var MAX_ARRAY_LENGTH = 4294967295;
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).
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.
25 * var diceRolls = _.times(3, _.partial(_.random, 1, 6, false));
28 * _.times(3, function(n) {
31 * // => invokes `mage.castSpell(n)` three times with `n` of `0`, `1`, and `2`
33 * _.times(3, function(n) {
36 * // => also invokes `mage.castSpell(n)` three times
38 function times(n, iteratee, thisArg) {
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)) {
47 result = Array(nativeMin(n, MAX_ARRAY_LENGTH));
49 iteratee = bindCallback(iteratee, thisArg, 1);
51 if (index < MAX_ARRAY_LENGTH) {
52 result[index] = iteratee(index);
60 module.exports = times;