-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (36 loc) · 1.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* eslint-env node */
'use strict'
var isInteger = require('validate.io-integer')
/**
* Synchronously calls `func` `n` number of times
*
* @name `call-n-times`
* @param {Function} `func` Called `n` times with the index as argument
* @param {Number} `n` Times `func` will be called
* @returns {Array} Return values of all the `func` calls
* @api public
*/
module.exports = function (func, n, cb) {
if (arguments.length < 2) {
throw new Error('Expected exactly 2 arguments')
}
if (typeof func !== 'function') {
throw new Error('Expected first argument to be a function')
}
if (typeof n !== 'number') {
throw new Error('Expected second argument to be a number')
}
if (n >= 0 !== true) {
throw new Error('Expected second argument to be equal to or greater than 0')
}
if (isInteger(n) !== true) {
throw new Error('Expected second argument to be an integer')
}
var returns = []
var returnVal
for (var i = 0; i < n; i++) {
returnVal = func(i) // passes index to provided `func`
returns.push(returnVal)
}
return returns
}