-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00-mock-api.js
51 lines (44 loc) · 1.24 KB
/
00-mock-api.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
44
45
46
47
48
49
50
51
const faker = require('faker');
const moment = require('moment');
const userProps = [
'id',
'firstName',
'lastName',
'email',
'title',
'notes',
'progress',
'active',
'startDate',
];
const getRandomInt = (min, max) => faker.datatype.number({ min, max });
const getRandomDate = () =>
moment().subtract(getRandomInt(3650, 7300), 'days').format('MM/DD/YYYY');
const generators = {
id: { func: faker.datatype.uuid },
firstName: { func: faker.name.firstName },
lastName: { func: faker.name.lastName },
email: { func: faker.internet.email },
title: { func: faker.random.words, args: 3 },
notes: { func: faker.lorem.words, args: 5 },
progress: { func: faker.datatype.number, args: { min: 0, max: 100 } },
active: { func: faker.datatype.boolean },
startDate: { func: getRandomDate },
};
const generate = (props, generators) => {
const obj = {};
props.forEach((prop) => {
const generator = generators[prop];
obj[prop] = generator.func(generator.args);
});
return obj;
};
const generateUsers = () => {
const minUsers = 3;
const maxUsers = 12;
const len = getRandomInt(minUsers, maxUsers);
const users = Array.apply(null, Array(len)).map(() =>
generate(userProps, generators)
);
return users;
};