-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
53 lines (43 loc) · 1.19 KB
/
helper.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
const generateRandomString = (stringLength) => {
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lower = upper.toLocaleLowerCase();
const digits = "0123456789";
const alphanumeric = upper + lower + digits;
let randomString = '';
for (let i = 0; i < stringLength; i++) {
randomString += alphanumeric[Math.floor(Math.random() * alphanumeric.length)];
}
return randomString;
};
const getUniqueVisitorCount = (id, database) => {
const visitors = [];
for (const visitor of database[id].visits) {
if (!visitors.includes(visitor)) {
visitors.push(visitor);
}
}
return visitors.length;
}
const getUserByKeyValue = (key, value, database) => {
for (const userID in database) {
if (database[userID][key] === value) {
return database[userID];
}
}
};
const urlsForUser = (id, database) => {
const newData = {};
for (const shortURL in database) {
if (database[shortURL].userID === id) {
newData[shortURL] = database[shortURL];
newData[shortURL].uniqueVisits = getUniqueVisitorCount(shortURL, database)
}
}
return newData;
};
module.exports = {
generateRandomString,
getUserByKeyValue,
urlsForUser,
getUniqueVisitorCount
};