-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.js
More file actions
191 lines (166 loc) · 5.45 KB
/
generator.js
File metadata and controls
191 lines (166 loc) · 5.45 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const fs = require('fs');
const casual = require('casual');
const consola = require('consola');
const { sub } = require('date-fns');
const treeImage = 'https://earthy.sauravmh.com/images/';
const trophyList = [
'Mr. Green',
'Earth Saviour',
'Novice Planter',
'Green thumb Jr.',
'Green Thumb Sr.',
'Earth Cleaner',
'Earth Cleaner Veteran',
'Environmentalist lvl 1',
'Environmentalist lvl 2',
'Environmentalist lvl 3',
'Recycling Enthusiast',
'Green Influencer Novice',
'Green Influencer Veteran',
];
const randomNumber = (min, max) => {
let num = Math.random() * (max - min) + min;
return Math.floor(num);
};
const randomTreeImage = () => {
const number = randomNumber(1, 14);
const image = treeImage + `${number}-min.jpg`;
return image;
};
const generator = (mode) => {
casual.seed(1);
let MAX_USERS = 100;
let MAX_POSTS = 1000;
let MAX_COMMENTS = 10;
let MAX_EVENTS = 100;
if (mode === 'development') {
MAX_USERS = 10;
MAX_POSTS = 10;
MAX_COMMENTS = 5;
MAX_EVENTS = 10;
}
const data = { users: [], posts: [], comments: [], events: [] };
casual.define('user', () => {
return {
firstName: casual.first_name,
lastName: casual.last_name,
email: casual.email,
phoneNo: casual.phone,
password: casual.password,
avatar: 'https://placeimg.com/640/480/people',
events: array_of(casual.integer(1, MAX_EVENTS / 4), 1, MAX_USERS),
points: Math.ceil((casual.integer(100, 5000) + 1) / 10) * 10,
trophies: getRandomSubarray(trophyList, casual.integer(1, 15)),
createdAt: casual.unix_time,
};
});
casual.define('event', () => {
const startTime = casual.unix_time;
return {
name: casual.title,
shortDescription: casual.short_description,
description: casual.description,
location: {
street: casual.street,
city: casual.city,
zipcode: casual.zipcode,
state: casual.state,
country: casual.country,
latitude: casual.latitude,
longitude: casual.longitude,
},
users: array_of(casual.integer(2, MAX_USERS / 4), 1, MAX_USERS),
startTime: startTime,
endTime: sub(startTime, {
days: casual.integer(1, 7),
}).toISOString(),
createdBy: casual.integer(1, 100),
createdAt: sub(startTime, {
days: casual.integer(7, 30),
}).toISOString(),
};
});
casual.define('post', () => {
return {
title: casual.title,
content: casual.description,
likes: casual.integer(100, 2500),
userId: casual.integer(1, MAX_USERS),
photo: randomTreeImage(),
createdAt: casual.unix_time,
};
});
casual.define('comment', () => {
return {
content: casual.sentence,
likes: casual.integer(1, 100),
postId: undefined, // To be added on runtime
userId: casual.integer(1, MAX_USERS),
createdAt: casual.unix_time,
};
});
// Create users
consola.success('Creating new users!');
for (let i = 1; i <= MAX_USERS; i++) {
data.users.push({ id: i, ...casual.user });
consola.debug(`Pushed user ${i}`);
}
consola.success(`${MAX_USERS} users generated!`);
// Create posts
consola.success('Creating new posts!');
let curr_comments_count = 0;
for (let i = 1; i <= MAX_POSTS; i++) {
data.posts.push({ id: i, ...casual.post });
consola.debug(`Pushed post ${i}`);
// Create comments
last_comments_count = curr_comments_count;
curr_comments_count += casual.integer(1, MAX_COMMENTS);
for (let j = last_comments_count; j <= curr_comments_count; j++) {
data.comments.push({ id: j, ...casual.comment, postId: i });
consola.debug(`Pushed comment ${j} for post ${i}`);
}
}
consola.success(
`${MAX_POSTS} posts with ${MAX_COMMENTS} comments each generated!`
);
// Create events
consola.success('Creating new events!');
for (let i = 1; i <= MAX_EVENTS; i++) {
data.events.push({ id: i, ...casual.event });
consola.debug(`Pushed event ${i}`);
}
consola.success(`${MAX_EVENTS} events generated!`);
return data;
};
const array_of = (size, start, end) => {
size = parseInt(size);
const result = [];
for (let i = 0; i < size; i++) {
let temp = casual.integer(start, end);
while (result.includes(temp)) {
temp = casual.integer(start, end);
}
result.push(temp);
}
return result;
};
const getRandomSubarray = (arr, size) => {
const shuffled = arr.slice(0);
let i = arr.length;
while (i--) {
const index = Math.floor((i + 1) * Math.random());
const temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(0, size);
};
const mode = process.env.NODE_ENV;
const data = generator(mode);
consola.success('Generation completed. Populating latest json into db.json');
fs.writeFile(
mode === 'development' ? 'dev_db.json' : 'db.json',
JSON.stringify(data),
'utf8',
() => consola.success('db.json populated!')
);