-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
51 lines (46 loc) · 1.24 KB
/
helper.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
/* Generates a random string, used for creating short URLs and userIDs */
function generateRandomString() {
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
};
// returns true if the email is already in use
const emailTaken = function(email, userDatabase) {
for (const user in userDatabase) {
if (userDatabase[user].email === email) {
return true;
}
}
return false;
};
// returns the id for the user
const userIdFromEmail = function(email, userDatabase) {
for (const user in userDatabase) {
if (userDatabase[user].email === email) {
return userDatabase[user].id;
}
}
};
// returns an object of short urls for the user
const urlsForUser = function(id, urlDatabase) {
const userUrls = {};
for (const shortURL in urlDatabase) {
if (urlDatabase[shortURL].userID === id) {
userUrls[shortURL] = urlDatabase[shortURL];
}
}
return userUrls;
};
// returns true if the current cookie is found in the users object
const userLoggedIn = function(cookie, userDatabase) {
for (const user in userDatabase) {
if (cookie === user) {
return true;
}
} return false;
};
module.exports = {
generateRandomString,
emailTaken,
userIdFromEmail,
urlsForUser,
userLoggedIn
};