-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsolution.js
76 lines (64 loc) · 1.81 KB
/
solution.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
"use strict";
const MY_DUMMY_URL = "http://localhost.com";
/**
*
* @param {number} id Number to convert
* @returns The reversed number in 62 format.
*/
function convertIdToShortenUrl(id) {
// 102 = 1 * 62^1 + 40 * 62^0 = bO
// 125 = 2 * 62^1 + 1 * 62^0 = cb
const algarisms =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let url = "";
while (id > 0) {
const algarismIndex = id % 62;
url += algarisms[algarismIndex];
id = Math.floor(id / 62);
}
return url; // This url needs to be reverted to be correctly displayed as base 62
}
/**
*
* @param {string} url Reversed number in 62 base
*/
function convertShortenUrlToId(url) {
let id = 0;
for (let i = url.length - 1; i >= 0; i--) {
if ("a" <= url[i] && url[i] <= "z") {
id = id * 62 + url.charCodeAt(i) - "a".charCodeAt(0);
} else if ("A" <= url[i] && url[i] <= "Z") {
id = id * 62 + url.charCodeAt(i) - "A".charCodeAt(0) + 26;
} else {
id = id * 62 + url.charCodeAt(i) - "0".charCodeAt(0) + 52;
}
}
return id;
}
function insertIntoDb(url) {
// fake insert into db
const fakeIdFromDb = 5e10;
console.log({ url, fakeIdFromDb });
return fakeIdFromDb;
}
function getFromDb(id) {
// fake get from db
if (id === 5e10) return MY_DUMMY_URL;
return null;
}
/**
*
* @param {string} url
*/
function shorten(url) {
const id = insertIntoDb(url); // db can also check if url already exists
return convertIdToShortenUrl(id);
}
function expand(shortUrl) {
const id = convertShortenUrlToId(shortUrl);
return getFromDb(id);
}
// Test
const shortenUrl = shorten(MY_DUMMY_URL);
const expanded = expand(shortenUrl);
console.log({ shortenUrl, expanded, worked: MY_DUMMY_URL === expanded });