HiddenPhox/src/lib/unicode.js

39 lines
899 B
JavaScript

const fetch = require("node-fetch");
const mappings = {};
async function cacheList() {
const data = await fetch(
"https://unicode.org/Public/UNIDATA/UnicodeData.txt"
).then((res) => res.text());
data
.split("\n")
.map((line) => line.split(";").splice(0, 2))
.forEach(([character, description]) => {
if (character != "") mappings[character.toLowerCase()] = description;
});
}
let getNamesFromString;
if (!global.____unicode_data) {
cacheList().then(() => {
global.____unicode_data = mappings;
getNamesFromString = function (string) {
const codes = [...string].map((char) => char.codePointAt().toString(16));
return codes.map((code) => [
code.padStart(4, "0"),
global.____unicode_data[code.padStart(4, "0")],
]);
};
});
}
module.exports = {
data: global.____unicode_data,
cacheList,
getNamesFromString,
};