-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfactory.js
40 lines (34 loc) · 1.03 KB
/
factory.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
const faker = require('faker')
/**
* Create a collection of mock data.
*
* The first parameter, numRecords, is used to define
* the number of records that should be created.
*
* The second parameter performs the user-defined
* callback which should return the data the user has
* defined. This can be anything: ints, floats, arrays,
* objects, you name it.
*
* An error will be thrown if the first parameter is
* not an integer or if the second parameter is not
* a function.
*
* @param {Number} numRecords
* @param {Function} callback
*
* @return {Array}
*
* @throws {Error}
*/
module.exports = (numRecords, callback) => {
// Validate first parameter is an integer
if (!Number.isInteger(numRecords)) throw new Error('First parameter must be an integer.')
// Validate second parameter is a function
if (!(callback instanceof Function)) throw new Error('Second parameter must be a function.')
let collection = []
for (let i = 0; i < numRecords; i++) {
collection.push(callback(faker, i))
}
return collection
}