| 1 |
var baseTimes = require('./_baseTimes'), |
| 2 |
castFunction = require('./_castFunction'), |
| 3 |
toInteger = require('./toInteger'); |
| 4 |
|
| 5 |
/** Used as references for various `Number` constants. */ |
| 6 |
var MAX_SAFE_INTEGER = 9007199254740991; |
| 7 |
|
| 8 |
/** Used as references for the maximum length and index of an array. */ |
| 9 |
var MAX_ARRAY_LENGTH = 4294967295; |
| 10 |
|
| 11 |
/* Built-in method references for those with the same name as other `lodash` methods. */ |
| 12 |
var nativeMin = Math.min; |
| 13 |
|
| 14 |
/** |
| 15 |
* Invokes the iteratee `n` times, returning an array of the results of |
| 16 |
* each invocation. The iteratee is invoked with one argument; (index). |
| 17 |
* |
| 18 |
* @static |
| 19 |
* @since 0.1.0 |
| 20 |
* @memberOf _ |
| 21 |
* @category Util |
| 22 |
* @param {number} n The number of times to invoke `iteratee`. |
| 23 |
* @param {Function} [iteratee=_.identity] The function invoked per iteration. |
| 24 |
* @returns {Array} Returns the array of results. |
| 25 |
* @example |
| 26 |
* |
| 27 |
* _.times(3, String); |
| 28 |
* // => ['0', '1', '2'] |
| 29 |
* |
| 30 |
* _.times(4, _.constant(0)); |
| 31 |
* // => [0, 0, 0, 0] |
| 32 |
*/ |
| 33 |
function times(n, iteratee) { |
| 34 |
n = toInteger(n); |
| 35 |
if (n < 1 || n > MAX_SAFE_INTEGER) { |
| 36 |
return []; |
| 37 |
} |
| 38 |
var index = MAX_ARRAY_LENGTH, |
| 39 |
length = nativeMin(n, MAX_ARRAY_LENGTH); |
| 40 |
|
| 41 |
iteratee = castFunction(iteratee); |
| 42 |
n -= MAX_ARRAY_LENGTH; |
| 43 |
|
| 44 |
var result = baseTimes(length, iteratee); |
| 45 |
while (++index < n) { |
| 46 |
iteratee(index); |
| 47 |
} |
| 48 |
return result; |
| 49 |
} |
| 50 |
|
| 51 |
module.exports = times; |