-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock-generator.js
150 lines (114 loc) · 3.72 KB
/
mock-generator.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const fs = require('fs-extra');
const CATEGORIES_AMOUNT = 30;
const DELIVERIES_AMOUNT = 50;
let categoryId = 1;
let productId = 1;
const rnd = (min, max, decimals = 0) => {
const m = Math.pow(10, decimals);
const number = Math.random() * (max - min + 1) + min;
return Math.floor(number * m) / m;
}
const getRandomItems = (arr, percentage = 0.5) => {
let items = arr.filter(() => Math.random() < percentage);
if (!items.length) {
const rndItem = arr[rnd(0, arr.length - 1)];
items = [rndItem];
}
return items;
};
const getRandomWords = (words, n = 1) =>
Array(n)
.fill(null)
.map(() =>
words[rnd(0, words.length - 1)]
.replace(/^./, (c) => c.toUpperCase())
);
const buildDelivery = (allCategories, words) => (v, deliveryIndex) => {
const categories = getRandomItems(allCategories, 0.1)
.map(category => ({
id: category.id,
products: getRandomItems(category.products)
}));
const name = getRandomWords(words, rnd(1, 3)).join(' ');
return {
categories,
id: deliveryIndex + 1,
name
};
};
const buildProducts = (words, n) =>
Array(n)
.fill(null)
.map(() => ({
id: productId++,
name: getRandomWords(words, rnd(1, 6)).join(' '),
price: rnd(1, 1000, 2)
}));
const writeCategoriesMock = async (productsByCategories, words) => {
const response = {};
response.results = productsByCategories.map(productsByCategory => {
const products = productsByCategory.response.results.map(product => product.id);
return {
id: productsByCategory.id,
name: getRandomWords(words, rnd(1, 5)).join(' '),
products
}
});
await fs.writeJson('./src/mockups/categories.json', response);
return response.results;
};
const writeAllDeliveriesMock = async (categories, words) => {
let response = {};
response.results = Array(DELIVERIES_AMOUNT)
.fill(null)
.map(buildDelivery(categories, words));
await fs.writeJson('./src/mockups/deliveries.json', response);
return response.results;
};
const writeDeliveriesMock = async (deliveries) => {
for (const delivery of deliveries) {
await fs.writeJson(`./src/mockups/deliveries/${delivery.id}.json`, delivery);
}
};
const writeProductsByCategoriesMock = async (words) => {
let response = {};
const productsByCategories = Array(CATEGORIES_AMOUNT)
.fill(null)
.map((v, index) => ({
id: index + 1,
response: {
results: buildProducts(words, rnd(1,10))
}
}));
for (const productsByCategory of productsByCategories) {
await fs.writeJson(
`./src/mockups/products-by-categories/${productsByCategory.id}.json`,
productsByCategory.response
);
}
return productsByCategories;
};
const writeMocks = async () => {
console.log('Removing mockups directory...');
await fs.remove('./src/mockups');
console.log('Making mockups directory...');
await fs.mkdirs('./src/mockups/deliveries');
await fs.mkdirs('./src/mockups/products-by-categories');
console.log('Reading mock text file...');
const file = await fs.readFile('./mock-text.txt', 'utf8');
const words = file
.replace(/\n/g, ' ')
.replace(/(\.|,)/g, '')
.split(' ')
.map(word => word.toLowerCase().trim());
console.log('Writing products by categories mock...');
const productsByCategories = await writeProductsByCategoriesMock(words);
console.log('Writing all categories mock...');
const categories = await writeCategoriesMock(productsByCategories, words);
console.log('Writing all deliveries mock...');
const deliveries = await writeAllDeliveriesMock(categories, words);
console.log('Writing deliveries mock...');
await writeDeliveriesMock(deliveries);
console.log('Mockups have been written successfully!');
};
writeMocks();