-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
206 lines (183 loc) · 5.25 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
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
const {
firstNames,
states,
lastNames,
emailProviders,
banks,
address,
networkProvider,
} = require("./names");
const WRONG_KEY_MESSAGE = "Did you forget to specify the key:";
const getName = (letterToStartWith, nameArray) => {
// nameArray can be either firstNames array or lastNames array
let max = nameArray.length - 1;
let min = 0;
let position;
if (!letterToStartWith) {
position = Math.floor(Math.random() * (max - min) + min);
return nameArray[position];
} else {
const arrayOfNamesThatStartsWithLetter = [];
nameArray.map((name) => {
if (name.startsWith(letterToStartWith)) {
arrayOfNamesThatStartsWithLetter.push(name);
}
});
max = arrayOfNamesThatStartsWithLetter.length;
min = 0;
position = Math.floor(Math.random() * (max - min) + min);
return arrayOfNamesThatStartsWithLetter[position];
}
}
const getFirstName = (letterToStartWith) => {
return getName(letterToStartWith, firstNames);
};
const getLastName = (letterToStartWith) => {
return getName(letterToStartWith, lastNames);
};
const getEmail = (fName, lName) => {
const min = 0;
const max = emailProviders.length;
const position = Math.floor(Math.random() * (max - min) + min);
return `${fName}.${lName}@${emailProviders[position]}`;
};
const getState = () => {
const min = 0;
const max = states.length;
const position = Math.floor(Math.random() * (max - min) + min);
return states[position].toLowerCase();
};
const getBank = () => {
const min = 0;
const max = banks.length;
const position = Math.floor(Math.random() * (max - min) + min);
return banks[position].toLowerCase();
};
// returns address
const getAddress = () => {
const houseNumber = getHouseNumber();
const streetAddress = getStreetAddress();
const city = getCity();
// returns address with house number, street and city
return `${houseNumber}, ${streetAddress}, ${city}`;
};
// returns House number
const getHouseNumber = () => {
const min = 0;
const max = address.houseNumber.length;
const position = Math.floor(Math.random() * (max - min) + min);
return address.houseNumber[position].toLowerCase();
};
// return street address
const getStreetAddress = () => {
const min = 0;
const max = address.streetAddress.length;
const position = Math.floor(Math.random() * (max - min) + min);
return address.streetAddress[position].toLowerCase();
};
// returns city
const getCity = () => {
const min = 0;
const max = address.city.length;
const position = Math.floor(Math.random() * (max - min) + min);
return address.city[position].toLowerCase();
};
// Randomly generates phone number based on the network provider
const getPhoneNumber = () => {
const countryCode = "+234";
// Generating network provider prefix
const networkPrefixArray = Object.keys(networkProvider);
const randomIndex = Math.floor(networkPrefixArray.length*Math.random());
const networkPrefix = networkPrefixArray[randomIndex];
// Generating the last part
const threeDigitPart = (Math.floor(Math.random() * 1000) + 1000).toString().substring(1);
const fourDigitPart = (Math.floor(Math.random() * 10000) + 10000).toString().substring(1);
// Full number
return `${countryCode}-${networkPrefix}-${threeDigitPart}-${fourDigitPart}`
};
// returns netowrk provider basd on the 2nd part of phonenumber
const getNetworkProvider = (phoneNumber) => {
const getNetworkPrefix = phoneNumber.split("-")[1];
return networkProvider[getNetworkPrefix];
}
const getPerson = (options) => {
let fName, lName, age;
if (!options) {
const min = 18;
const max = 50;
fName = getFirstName();
lName = getLastName();
age = Math.floor(Math.random() * (max - min) + min);
phoneNumber = getPhoneNumber();
return {
fName,
lName,
age,
email: getEmail(fName, lName),
state: getState(),
bank: getBank(),
address: getAddress(),
phoneNumber,
networkProvider: getNetworkProvider(phoneNumber),
};
} else {
let { min, max } = options;
if (!min && !max) {
return WRONG_KEY_MESSAGE + "'min' and 'max'";
}
fName = getFirstName();
lName = getLastName();
age = Math.floor(Math.random() * (max - min) + min);
phoneNumber = getPhoneNumber();
return {
fName,
lName,
age,
email: getEmail(fName, lName),
state: getState(),
bank: getBank(),
address: getAddress(),
phoneNumber,
networkProvider: getNetworkProvider(phoneNumber),
};
}
};
const getPersonList = (options) => {
let listCount = 5;
if (options) {
const { amt } = options;
if (!amt) return WRONG_KEY_MESSAGE + "'amt'";
listCount = amt;
}
const personArr = [];
new Array(listCount).fill(0).map(() => {
personArr.push(getPerson());
});
return personArr;
};
const getNameList = (options) => {
let listCount = 5;
if (options) {
const { amt } = options;
if (!amt) return WRONG_KEY_MESSAGE + "'amt'";
listCount = amt;
}
const firstNamesArr = [];
new Array(listCount).fill(0).map(() => {
firstNamesArr.push(getFirstName());
});
return firstNamesArr;
};
module.exports = {
getFirstName,
getLastName,
getPerson,
getPersonList,
getNameList,
getBank,
getAddress,
getState,
getEmail,
getPhoneNumber,
getNetworkProvider,
};