-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookupURL.js
89 lines (81 loc) · 2.36 KB
/
lookupURL.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const database = {
g : {
shortURL : "g",
longURL : "https://www.google.ca",
userID : "userID",
lastUpdated : Date.now(),
'visits' : Math.floor(Math.random() * 500)
},
LH : {
shortURL : "LH",
longURL : "https://www.lighthouselabs.ca",
userID : "userID",
lastUpdated : Date.now(),
'visits' : Math.floor(Math.random() * 500)
},
example : {
shortURL : "example",
longURL : "https://www.example.com",
userID : "example",
lastUpdated : Date.now(),
'visits' : Math.floor(Math.random() * 500)
}
};
const addURL = (shortURL, longURL, userID) => {
shortURL = typeof(shortURL) === 'string' ? shortURL : undefined;
longURL = typeof(longURL) === 'string' ? longURL : undefined;
userID = typeof(userID) === 'string' ? userID : undefined;
if (shortURL && longURL && userID) {
database[shortURL] = {
shortURL,
longURL,
userID,
lastUpdated : Date.now(),
'visits' : 0
};
return true;
} else {
return false;
}
};
const removeURL = (shortURL, userID) => {
shortURL = typeof(shortURL) === 'string' ? shortURL : undefined;
userID = typeof(userID) === 'string' ? userID : undefined;
let owner = typeof(database[shortURL].userID) === 'string' ? database[shortURL].userID : 'unknownOwner';
if (shortURL && userID === owner) {
return delete database[shortURL];
} else {
return false;
}
};
const getURL = (shortURL, incrementVisit = false) => {
shortURL = typeof(shortURL) === 'string' ? shortURL : undefined;
incrementVisit = typeof(increment) === 'boolean' ? incrementVisit : false;
if (shortURL && database[shortURL]) {
if (incrementVisit) {
database[shortURL].visits += 1;
}
return database[shortURL];
} else {
return false;
}
};
const updateURL = (shortURL, longURL, userID) => {
shortURL = typeof(shortURL) === 'string' ? shortURL : undefined;
longURL = typeof(longURL) === 'string' ? longURL : undefined;
userID = typeof(userID) === 'string' ? userID : undefined;
let owner = typeof(database[shortURL].userID) === 'string' ? database[shortURL].userID : 'unknownOwner';
if (shortURL && longURL && userID && userID === owner) {
database[shortURL].longURL = Date.now();
database[shortURL].longURL = longURL;
return true;
} else {
return false;
}
};
module.exports = {
addURL,
removeURL,
updateURL,
getURL
};