-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
46 lines (40 loc) · 1.19 KB
/
Copy pathhelpers.js
File metadata and controls
46 lines (40 loc) · 1.19 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
// Random string
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const generateRandomString = function(length) {
let result = " ";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
// Helper function to get user by email
const getUserByEmail = function(users, email) {
for (const userID in users) {
const user = users[userID];
if (user.email === email) {
return user;
}
}
return null;
};
// helper function
const urlsForUser = function(id, urlDatabase) {
const userURLs = {};
for (const shortURL in urlDatabase) {
if (urlDatabase[shortURL].userID === id) {
userURLs[shortURL] = urlDatabase[shortURL]["longURL"];
}
}
return userURLs;
};
// Helper function to get URL specific to the user by ID
const getUserURLByID = (userId, shortURL, urlDatabase) => {
const { longURL, userID } = urlDatabase[shortURL];
if (longURL && userID === userId) {
return { longURL, userID };
}
return null;
};
module.exports = { getUserByEmail, generateRandomString, urlsForUser, getUserURLByID };