HiddenPhox/src/lib/unicode.js

33 lines
765 B
JavaScript
Raw Normal View History

2021-03-15 04:23:14 +00:00
const mappings = {};
async function cacheList() {
const data = await fetch(
2022-12-20 18:27:26 +00:00
"https://www.unicode.org/Public/UNIDATA/UnicodeData.txt"
2021-03-15 04:23:14 +00:00
).then((res) => res.text());
data
.split("\n")
.map((line) => line.split(";").splice(0, 2))
.forEach(([character, description]) => {
if (character != "") mappings[character.toLowerCase()] = description;
});
global.____unicode_data = mappings;
2021-03-15 04:23:14 +00:00
}
async function getNamesFromString(string) {
if (!global.____unicode_data) await cacheList();
2021-03-15 04:23:14 +00:00
const codes = [...string].map((char) => char.codePointAt().toString(16));
2021-03-15 04:23:14 +00:00
return codes.map((code) => [
code.padStart(4, "0"),
global.____unicode_data[code.padStart(4, "0")],
]);
2021-03-15 04:23:14 +00:00
}
module.exports = {
cacheList,
getNamesFromString,
};