-
Notifications
You must be signed in to change notification settings - Fork 8
/
url-lengthener-node.js
115 lines (104 loc) · 2.25 KB
/
url-lengthener-node.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//var exports = {"__esModule": true};
const hex2str = (hexx) => {
var hex = hexx.toString();//force conversion
var str = '';
for (var i = 0; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
const str2hex = (str) => {
var arr = [];
for (var i = 0, l = str.length; i < l; i ++) {
var hex = Number(str.charCodeAt(i)).toString(16);
arr.push(hex);
}
return arr.join('');
}
const hex2a = (str) => {
let arr_hex = str.split('');
let reformattedArray = arr_hex.map(char => {
const replacements = {
'0': 'a',
'1': 'à',
'2': 'á',
'3': 'â',
'4': 'ã',
'5': 'ä',
'6': 'å',
'7': 'æ',
'8': 'A',
'9': 'À',
'a': 'Á',
'b': 'Â',
'c': 'Ã',
'd': 'Ä',
'e': 'Å',
'f': 'Æ'
};
return replacements[char];
})
return reformattedArray.join('');
}
const a2hex = (str) => {
let arr_hex = str.split('');
let reformattedArray = arr_hex.map(char => {
const replacements = {
'a': '0',
'à': '1',
'á': '2',
'â': '3',
'ã': '4',
'ä': '5',
'å': '6',
'æ': '7',
'A': '8',
'À': '9',
'Á': 'a',
'Â': 'b',
'Ã': 'c',
'Ä': 'd',
'Å': 'e',
'Æ': 'f'
};
return replacements[char];
})
return reformattedArray.join('');
}
const validURL = (url) => {
try {
new URL(url);
} catch (e) {
console.error(e);
return false;
}
return true;
};
const lengthen = (url) => {
if (validURL(url)) {
new_url = hex2a(str2hex(url));
while (new_url.length < 200) {
new_url = "áaaÂ" + new_url;
}
return "https://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com/a?" + new_url;
} else {
throw new Error("The URL passed is not valid.");
}
}
const lengthenAsPath = (url) => {
if (validURL(url)) {
new_url = hex2a(str2hex(url));
while (new_url.length < 200) {
new_url = "áaaÂ" + new_url;
}
return "https://api.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com/" + new_url;
} else {
throw new Error("The URL passed is not valid.");
}
}
exports.hex2str = hex2str;
exports.str2hex = str2hex;
exports.hex2a = hex2a;
exports.a2hex = a2hex;
exports.validURL = validURL;
exports.lengthen = lengthen;
exports.lengthenAsPath = lengthenAsPath;