-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
144 lines (122 loc) · 3.2 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🔎</text></svg>">
<title>Offline unicode character inspector</title>
<style>
body {
width: 56rem;
margin: 0 auto;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
h1, p {
margin: 0;
}
textarea {
width: 32rem;
height: 6ch;
}
table {
border-collapse: collapse;
border: 2px solid black;
table-layout: auto;
}
th, td {
border: 1px solid black;
}
td {
font-family: monospace;
}
.wide {
width: 32%;
}
.u16 {
width: 9ch;
}
.u8 {
width: 11ch;
}
</style>
</head>
<body>
<h1>Unicode inspector</h1>
<p>Low-fi and entirely offline - no data is submitted to any server</p>
<textarea id="the-textarea"></textarea>
<span>
<input type="checkbox" id="deduplicate" name="deduplicate" checked />
<label for="deduplicate">Hide duplicates</label>
</span>
<table>
<thead>
<tr id="heads">
<th>Character</th>
<th>Codepoint</th>
<th class="wide">Name</th>
<th class="wide">Extra</th>
<th class="u16">UTF-16BE</th>
<th class="u8">UTF-8</th>
</tr>
</thead>
<tbody id="the-table">
</tbody>
</table>
<script>
const a = fetch('./UnicodeData.txt')
.then(res => res.text())
.then(lines => lines.split('\n'))
.then(lines => lines.map(l => {
const data = l.split(';');
const codepoint = data[0];
const codepointN = parseInt(data[0], 16);
const name = data[1];
const extra = data[10];
return [codepointN, codepoint, name, extra];
}));
const calcU16 = (s) => {
const out = [];
for (let i = 0; i < s.length; i++) {
const x = s.charCodeAt(i);
if (isNaN(x)) break;
out.push(zeropad(x.toString(16), 4));
}
return out;
};
const textencoder = new TextEncoder();
const zeropad = (s, n) => '0'.repeat(Math.max(0, n - s.length)) + s;
const onInput = () => {
const tbody = document.getElementById('the-table');
const deduplicate = document.getElementById('deduplicate').checked;
while (tbody.firstChild) {
tbody.removeChild(tbody.lastChild);
}
a.then(unicodeData => {
const data = document.getElementById('the-textarea').value;
const seenChars = new Set();
for (const char of data) {
if (deduplicate && seenChars.has(char)) continue;
seenChars.add(char);
const row = document.createElement('tr');
const codepoint = char.codePointAt(0);
const match = unicodeData.find(u => u[0] === codepoint);
const u16 = calcU16(char).join(' ');
const u8 = Array.from(textencoder.encode(char))
.map(b => zeropad(b.toString(16), 2))
.join(' ');
const rowItems = [char, ...match.slice(1, 4), u16, u8];
for (const item of rowItems) {
const td = document.createElement('td');
row.appendChild(td);
td.innerText = item;
}
tbody.appendChild(row);
}
});
};
document.getElementById('the-textarea').addEventListener('input', onInput);
document.getElementById('deduplicate').addEventListener('input', onInput);
</script>
</body>
</html>