-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (50 loc) · 1.58 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { v4 } from "uuid";
/**
* INPUT: a javascript array of objects
* OUTPUT: the same array, but every object has a new property
* with key: _id and value: a randomly generated string
*/
const makeId = (array) => {
array.map((d) => (d["_id"] = v4()));
return array;
};
/**
* INPUT: a javascript array of objects and a preferred name for key id
* OUTPUT: the same array, but every object has a new property
* with key: _id and value: a randomly generated string
*/
const makeIdCustom = (array, key) => {
array.map((a) => (a[key] = v4()));
return array;
};
/**
* INPUT: a javascript array of objects and number of new mock
* objects desired
* OUTPUT: the same array, plus the n new objects that resemble the original objects
*/
const makeMock = (array, nObj) => {
const protoL = Object.keys(array[0]).length;
const mock = array;
for (let i = 0; i < nObj; i++) {
const obj = {};
for (let j = 0; j < protoL; j++) {
const protoF = Object.keys(array[0])[j];
if (!isNaN(parseInt(array[0][protoF]))) {
obj[protoF] = Math.floor(Math.random() * 100 + 1);
} else if (
typeof array[0][protoF] === "string" &&
array[0][protoF].includes("http")
)
obj[protoF] = "https://it.wikipedia.org/wiki/Monte_Rosa";
else obj[protoF] = "Lorem ipsum dolor sit amet";
}
mock.push(obj);
}
return mock;
};
const jcereal = {
makeId: makeId,
makeIdCustom: makeIdCustom,
makeMock: makeMock,
};
export default jcereal;