-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
46 lines (42 loc) · 1.24 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
42
43
44
45
46
// takes in number of char desired, generates a random string alpha-numerically
const generateRandomId = function(numOfChars) {
const allChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let randomString = "";
for (let i = 0; i < numOfChars; i++) {
randomString += allChars[Math.floor(Math.random() * allChars.length)];
}
return randomString;
};
// takes in an email and database, returns user ID if user email exists in "users" DB
const getUserByEmail = function(email, db) {
for (let account in db) {
if (db[account]["email"] === email) {
return account;
}
}
};
// filters a urlDatabase for those that have a matching userID to user logged in
const urlsForUserId = function(userId, urlDB) {
let urlsFiltered = {};
for (const url in urlDB) {
if (urlDB[url]["userID"] === userId) {
urlsFiltered[url] = {
longURL: urlDB[url]["longURL"],
visitCount: urlDB[url]["visitCount"],
timeStamp: urlDB[url]["timeStamp"]
};
}
}
return urlsFiltered;
};
// create date of entry
const timeStamp = function() {
let currentTime = new Date().toDateString();
return currentTime;
};
module.exports = {
generateRandomId,
getUserByEmail,
urlsForUserId,
timeStamp
};