-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlConverter.js
72 lines (70 loc) · 1.74 KB
/
urlConverter.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
function shortenUrl(url) {
var split = url.split("/");
var types = ["users", "projects", "studios", "discuss"];
if (types.indexOf(split[3]) >= 0) {
if (split[3] == "discuss") {
var id = split[5];
} else {
var id = split[4];
}
var type = ["u", "p", "s", "d"][types.indexOf(split[3])];
if (type != "u") {
id = base62.encode(id);
}
if (type == "p" && split[5] == "editor") {
return "https://gobo.cf/e#" + id;
} else {
return "https://gobo.cf/" + type + "#" + id;
}
} else {
return "Unvalid URL";
}
}
function longUrl(url) {
var urlStarts = [
"https://scratch.mit.edu/users/",
"https://scratch.mit.edu/projects/",
"editor",
"https://scratch.mit.edu/studios/",
"https://scratch.mit.edu/discuss/topic/"
];
var data = url.split("/");
var type = urlStarts[["u", "p", "e", "s", "d"].indexOf(data[3])];
if (type == "https://scratch.mit.edu/users/") {
return type + data[4].replace("#", "");
} else {
if (type == "editor") {
return (
"https://scratch.mit.edu/projects/" +
base62.decode(data[4].replace("#", "")) +
"/editor"
);
} else {
return type + base62.decode(data[4].replace("#", ""));
}
}
}
const base62 = {
charset: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_".split(
""
),
encode: integer => {
if (integer === 0) {
return "0";
}
let s = [];
while (integer > 0) {
s = [base62.charset[integer % 62], ...s];
integer = Math.floor(integer / 62);
}
return s.join("");
},
decode: chars =>
chars
.split("")
.reverse()
.reduce(
(prev, curr, i) => prev + base62.charset.indexOf(curr) * 62 ** i,
0
)
};