-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
231 lines (222 loc) · 6.73 KB
/
db.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const { surnames, maleNames, femaleNames } = require("./_names");
const { nouns, colors, adjectives, emailDomains } = require("./_words");
const genders = ["male", "female"];
const usernames = [];
const pwdRegExp = new RegExp(
"^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})"
);
const _getUniqueWords = (arr) => {
return Array.from(new Set(arr));
};
const words = [
_getUniqueWords(nouns),
_getUniqueWords(adjectives),
_getUniqueWords(colors),
].flat();
const _userIds = new Set();
const addUserToDb = (user) => {
try {
const generateNewId = () => {
let id = Math.floor(Math.random() * _userIds.size + 1);
if (_userIds.has(id)) {
return generateNewId();
}
return id;
};
if (users.find((u) => u.email === user.email)) {
return null;
}
user.id = generateNewId();
_userIds.add(user.id);
users.push(user);
return user;
} catch (error) {
console.log(error);
return null;
}
};
class UserGenerator {
constructor(id) {
this.id = id;
_userIds.add(id);
this.name = this._generateName();
this.email = this._generateEmail();
this.password = this._generatePassword();
this.birthday = this._generateBirthday();
this.phone = this._generatePhone();
}
_generateName() {
this.name = `${this._generateFirstName()} ${this._generateLastName()}`;
return this.name;
}
_generateFirstName() {
this.gender = genders[Math.floor(Math.random() * genders.length)];
switch (this.gender) {
case genders[0]:
return maleNames[Math.floor(Math.random() * maleNames.length)];
case genders[1]:
return femaleNames[Math.floor(Math.random() * femaleNames.length)];
default:
return "Unknown";
}
}
_generateLastName() {
return surnames[Math.floor(Math.random() * surnames.length)];
}
_generateEmail() {
return `${this._generateUsername()}@${
emailDomains[Math.floor(Math.random() * emailDomains.length)]
}`;
}
_generateUsername() {
let _username = "";
for (let i = 0; i < Math.floor(Math.random() * words.length); i++) {
let rollDice = Math.floor(Math.random() * 7);
switch (rollDice) {
case 0:
// user first initial and last name is used as username
_username = `${this.name[0].toLowerCase()}${this.name
.split(" ")[1]
.toLowerCase()}`;
break;
case 1:
// user initials are used as username with random number
_username = `${this.name[0].toLowerCase()}${this.name
.split(" ")[1][0]
.toLowerCase()}${Math.floor(Math.random() * 1000)}`;
break;
case 2:
// random word is used as username
_username = `${words[
Math.floor(Math.random() * words.length)
].toLowerCase()}`;
break;
case 3:
// random word is used as username with random number
_username = `${words[
Math.floor(Math.random() * words.length)
].toLowerCase()}${Math.floor(Math.random() * 1000)}`;
break;
case 4:
// parts of random words are used as username
_username = `${words[
Math.floor(Math.random() * words.length)
].toLowerCase()}${words[
Math.floor(Math.random() * words.length)
].toLowerCase()}`;
break;
case 5:
// parts of random words are used as username with parts of name and random number
_username = `${words[
Math.floor(Math.random() * words.length)
].toLowerCase()}${words[
Math.floor(Math.random() * words.length)
].toLowerCase()}${this.name[0].toLowerCase()}${this.name
.split(" ")[1][0]
.toLowerCase()}${Math.floor(Math.random() * 1000)}`;
break;
case 6:
// user first and last name is used separated by a dot and random characters or numbers are sometimes added
if (
Math.floor(Math.random() * 2) === 0 &&
Math.floor(Math.random() * 2) === 0
) {
_username = `${this.name.replace(" ", ".").toLowerCase()}`;
} else if (Math.floor(Math.random() * 2) === 0) {
_username = `${this.name
.replace(" ", ".")
.toLowerCase()}${Math.floor(Math.random() * 1000)}`;
} else {
const chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._";
_username = `${this.name.replace(" ", ".").toLowerCase()}${
chars[Math.floor(Math.random() * chars.length)]
}${Math.floor(
(Math.random() * 1000) % 2 === 0
? Math.floor(Math.random() * 1000)
: ""
)}`;
}
break;
default:
// user first and last name is used as username
_username = this.name.replace(" ", "").toLowerCase();
break;
}
}
if (usernames.includes(_username) || ["", " ", "."].includes(_username)) {
return this._generateUsername();
} else {
usernames.push(_username);
return _username;
}
}
_generatePassword() {
// make password to match pwdRegExp const above
let _password = "";
while (!_password.match(pwdRegExp)) {
_password = "";
for (let i = 0; i < Math.floor(Math.random() * 10) + 8; i++) {
_password += String.fromCharCode(Math.floor(Math.random() * 94) + 33);
}
}
return _password;
}
_generateBirthday() {
const birthday = new Date();
birthday.setFullYear(Math.floor(Math.random() * 100) + 1920);
birthday.setMonth(Math.floor(Math.random() * 12));
birthday.setDate(Math.floor(Math.random() * 28));
this.birthday = birthday.toISOString().split("T")[0];
return this.birthday;
}
_generatePhone() {
const phone = new Array(10);
for (let i = 0; i < phone.length; i++) {
phone[i] = Math.floor(Math.random() * 10);
}
const formatPhoneNumber = (phone) =>
`+1(${phone.slice(0, 3).join("")})${phone.slice(3, 6).join("")}-${phone
.slice(6)
.join("")}`;
return formatPhoneNumber(phone);
}
getName() {
return this.name;
}
getEmail() {
return this.email;
}
getPassword() {
return this.password;
}
getBirthday() {
return this.birthday;
}
getGender() {
return this._gender;
}
getPhone() {
return this.phone;
}
getId() {
return this.id;
}
setId(id) {
this.id = id;
}
}
const seedUsers = (num = 10) => {
const users = [];
for (let i = 0; i < num; i++) {
const user = new UserGenerator(i);
users.push(user);
}
return users;
};
const users = seedUsers(100);
module.exports = {
users: users,
seedUsers: seedUsers,
addUserToDb: addUserToDb,
};