-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chars.ts
30 lines (25 loc) · 936 Bytes
/
chars.ts
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
import { readdir, readFile } from 'node:fs/promises';
async function go() {
const chars = new Map<string, number>();
const files = await readdir("src", { recursive: true });
for (const file of files) {
if (!file.endsWith(".md")) {
continue;
}
const content: String = await readFile(`src/${file}`, { encoding: 'utf8' });
for (const c of [...content]) {
const current = chars.get(c) || 0;
chars.set(c, current + 1);
}
}
const sorted = [...chars.entries()].sort((a, b) => b[1] - a[1]);
const is_printable = /[\p{Letter}\p{Number}\p{Punctuation}\p{Symbol}]/u;
for (const [char, count] of sorted) {
if (char.match(is_printable)) {
console.log(`${char}: ${count}`);
} else {
console.log(`U+${char.codePointAt(0)?.toString(16).padStart(4, '0')}: ${count}`);
}
}
}
go();