-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
41 lines (34 loc) · 1.06 KB
/
helpers.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
// helper function to look up if email address exsits in the object database
const getUserByEmail = (email, database) => {
for (let user in database) {
const userObj = database[user];
if (userObj.email === email) {
return userObj;
}
}
};
// function that returns a string of 6 random alphanumeric characters
const generateRandomString = () => {
const arr = '0123456789abcdefghijklmnopqrstuvwxyz';
let result = '';
for (let i = 6; i > 0; i--) {
result += arr[Math.floor(Math.random() * arr.length)];
}
return result;
};
// Function which returns the URLs where the userID is equal to the id of the currently logged-in user
const urlsForUser = (id, database) => {
const urlArr = [];
// Display only URLs shortened by logged in user
for (let shortURL in database) {
if (database[shortURL].userID === id) {
const urlObj = {
shortURL: shortURL,
longURL: database[shortURL].longURL
};
urlArr.push(urlObj);
}
}
return urlArr;
};
module.exports = { getUserByEmail, generateRandomString, urlsForUser };