-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
39 lines (35 loc) · 983 Bytes
/
helpers.js
File metadata and controls
39 lines (35 loc) · 983 Bytes
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
//helper functions:
//function takes in user id and url database,
//and returns an object containing just the url objects
//that belong to specific user
const urlsForUser = function (id, database) {
const urls = {};
for (let shortURL in database) {
if (database[shortURL].userId === id) {
urls[shortURL] = database[shortURL];
}
}
return urls;
}
//function generates random 6-character string,
//to be used for short url or user id
const generateRandomString = function () {
let string = Math.random().toString(36).slice(2, 8);
return string;
};
//function takes in an email, loops through user database,
//and returns user object that matches email
//if no match, returns undefined
const getUserByEmail = function (email, database) {
for (let userId in database) {
if (database[userId].email === email) {
return database[userId];
}
}
return undefined;
}
module.exports = {
urlsForUser,
generateRandomString,
getUserByEmail
}