Last active
June 3, 2022 01:07
-
-
Save bpevs/29ea0a4374f4f2746761deee8cb9c9aa to your computer and use it in GitHub Desktop.
Parse and build a js obj with the unicode data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Builds obj from unicode emoji-test text | |
// https://unicode.org/Public/emoji/14.0/emoji-test.txt | |
// http://www.unicode.org/reports/tr51/ | |
// Usage: | |
// deno run --allow-net fetch-emoji.ts | |
type Qualification = "fully-qualified" | "minimally-qualified" | "unqualified"; | |
interface EmojiObj { | |
emoji: string; | |
version: number; | |
name: string; | |
qualification: Qualification; | |
codepoints: string[]; | |
ln?: string; | |
} | |
interface Results { | |
[groupName: string]: { | |
[subgroupName: string]: EmojiObj[]; | |
}; | |
} | |
const version = "14.0"; | |
const response = await fetch( | |
`https://unicode.org/Public/emoji/${version}/emoji-test.txt`, | |
); | |
const text = await response.text(); | |
const groups = text.split("# group: "); | |
groups.shift(); | |
const results: Results = {}; | |
groups.forEach((group) => { | |
const [rawGroupName, ...subgroups] = group.split("# subgroup: "); | |
const groupName = rawGroupName.replace("\n\n", ""); | |
results[groupName] = {}; | |
subgroups.map((subgroup) => { | |
const [subgroupName, ...emojis] = subgroup.split("\n"); | |
const emojiObjs = emojis | |
.map((ln): EmojiObj => { | |
const [_, codepoints, qualification, emoji, version, name] = ln.match( | |
/^(.*);(.*)#(.*)E(\S*)\s(.*)/, | |
) || []; | |
return { | |
emoji: (emoji || "").trim(), | |
version: Number(version), | |
name: name, | |
qualification: (qualification || "").trim() as Qualification, | |
codepoints: (codepoints || "").trim().split(/\s+/), | |
// ln: ln, | |
}; | |
}) | |
.filter((emojiObj) => | |
emojiObj.emoji && emojiObj.qualification === "fully-qualified" | |
); | |
results[groupName][subgroupName] = emojiObjs; | |
}); | |
}); | |
console.log(results); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment