-
Notifications
You must be signed in to change notification settings - Fork 0
/
shorten.js
220 lines (191 loc) · 5.94 KB
/
shorten.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
async function fetchTable() {
const response = await fetch('http://www.unicode.org/Public/idna/latest/IdnaMappingTable.txt');
return response.text();
}
function int2unicode(i) {
return String.fromCodePoint(i);
}
function code2unicode(code) {
return int2unicode(parseInt(code, 16));
}
function parseTable(table) {
// split and strip header
const lines = table.split('\n').slice(11);
const entries = Object.create(null);
for (const line of lines) {
const match = /^([0-9A-F]{4,}(?:\.\.[0-9A-F]{4,})?)\s+;\s+([^;#]+?)\s+(?:;((?: [0-9A-F]{4,})+)\s+)?#.+$/.exec(line);
if (!match) {
continue;
}
const charDescriptor = match[1];
const state = match[2].trim();
if (state !== 'mapped') {
continue;
}
let chars = [];
if (charDescriptor.includes('..')) {
const [start, end] = charDescriptor.split('..').map(s => parseInt(s, 16));
for (let i = start; i <= end; i++) {
chars.push(int2unicode(i));
}
} else {
chars.push(code2unicode(charDescriptor));
}
const mapping = match[2].trim().split(' ');
for (const char of chars) {
entries[char] = mapping.map(code2unicode).join('');
}
}
return entries;
}
function createLookup(parsed) {
const reverse = Object.create(null);
for (const [key, value] of Object.entries(parsed)) {
if (value in reverse) {
reverse[value].push(key);
} else {
reverse[value] = [key];
}
}
return reverse;
}
function optimize(lookup) {
const optimized = Object.create(null);
for (const [long, shortcuts] of Object.entries(lookup)) {
for (const shortcut of shortcuts) {
if (shortcut.length < long.length) {
if (!(long in optimized) || (shortcut.length < optimized[long][0].length)) {
// shortcut is the first or shorter
optimized[long] = [shortcut];
} else if (shortcut.length === optimized[long][0].length) {
// shortcut is not longer than the previously found one
optimized[long].push(shortcut);
}
}
}
}
return optimized;
}
class Node {
constructor(value) {
this.value = value;
this.id = null;
}
}
class DirectedAcyclicGraph {
constructor() {
this.nodes = [];
this.edgesTo = new Map();
this.edgesFrom = new Map();
}
addNode(value=null) {
const node = new Node(value);
this.nodes.push(node);
node.id = this.nodes.length - 1;
return node;
}
addEdge(nodeFrom, nodeTo) {
if (this.edgesFrom.has(nodeFrom)) {
this.edgesFrom.get(nodeFrom).push(nodeTo);
} else {
this.edgesFrom.set(nodeFrom, [nodeTo]);
}
if (this.edgesFrom.has(nodeTo)) {
this.edgesTo.get(nodeTo).push(nodeFrom);
} else {
this.edgesTo.set(nodeTo, [nodeFrom]);
}
}
predecessors(node) {
if (!this.edgesTo.has(node)) {
return [];
}
return [...this.edgesTo.get(node)];
}
successors(node) {
if (!this.edgesFrom.has(node)) {
return [];
}
return [...this.edgesFrom.get(node)];
}
}
function shortenOptimal(lookup, string) {
const g = new DirectedAcyclicGraph();
// insert starting point
const start = g.addNode();
// insert all 'regular' nodes + edges
const charNodes = [];
let last = start;
for (const char of string) {
const n = g.addNode(char);
charNodes.push(n);
g.addEdge(last, n);
last = n;
}
// insert destination point
const end = g.addNode();
g.addEdge(last, end);
// insert all shortcuts
for (const [long, shortcuts] of Object.entries(lookup)) {
// select one of the possible shortest shortcuts
const shortcut = shortcuts[0];
const n = g.addNode(shortcut);
// check all occurrences
let startPos = 0;
while (true) {
startPos = string.indexOf(long, startPos);
if (startPos === -1) {
break;
}
// insert a shortcut
const startNode = charNodes[startPos];
const endNode = charNodes[startPos + long.length - 1];
for (const fromNode of g.predecessors(startNode)) {
for (const toNode of g.successors(endNode)) {
g.addEdge(fromNode, n);
g.addEdge(n, toNode);
}
}
startPos += 1;
}
}
// prepare
const pred = { start: null };
const dist = { start: 0 };
// find shortest path
const q = [start];
while (q.length > 0) {
const current = q.shift();
for (const succ of g.successors(current)) {
const distance = dist[current] + 1;
if (succ in dist) {
if (distance < dist[succ]) {
dist[succ] = distance;
pred[succ] = current;
}
} else {
dist[succ] = distance;
pred[succ] = current;
}
q.push(succ);
}
}
// reconstruct path
const path = [];
let current = end;
while (current !== null) {
path.push(current);
current = pred[current];
}
return path.slice(1, path.length - 1).reverse().map(n => n.value).join('');
}
async function shortenUrl(url) {
const table = await fetchTable();
const parsed = parseTable(table);
const lookup = createLookup(parsed);
const optimized = optimize(lookup);
const shorteneDomain = shortenOptimal(optimized, new URL(url).hostname);
const shortenedUrl = new URL(url);
shortenedUrl.hostname = shorteneDomain;
return shortenedUrl.toString();
}