|
1 |
| -import { defineMojiAdapter } from "."; |
| 1 | +import type { Emoji, EmojiData, EmojiSequence, EmojiVariation, Property } from "../types"; |
| 2 | +import { defineMojiAdapter } from "../adapter"; |
| 3 | +import { FEMALE_SIGN, MALE_SIGN } from "../constants"; |
| 4 | +import { extractEmojiVersion, extractUnicodeVersion } from "../utils"; |
| 5 | +import { fetchCache } from "../utils/cache"; |
| 6 | +import { expandHexRange } from "../utils/hexcode"; |
2 | 7 |
|
3 | 8 | export default defineMojiAdapter({
|
4 | 9 | name: "v13",
|
5 | 10 | description: "adapter for version 13 & 13.1",
|
6 | 11 | range: ">=13.0.0 <14.0.0",
|
7 | 12 | extend: "base",
|
| 13 | + sequences: async (ctx) => { |
| 14 | + const [sequences, zwj] = await Promise.all([ |
| 15 | + { |
| 16 | + cacheKey: `v${ctx.emojiVersion}/sequences.json`, |
| 17 | + url: `https://unicode.org/Public/emoji/${ctx.emojiVersion}/emoji-sequences.txt`, |
| 18 | + }, |
| 19 | + { |
| 20 | + cacheKey: `v${ctx.emojiVersion}/zwj-sequences.json`, |
| 21 | + url: `https://unicode.org/Public/emoji/${ctx.emojiVersion}/emoji-zwj-sequences.txt`, |
| 22 | + }, |
| 23 | + ].map(async ({ cacheKey, url }) => { |
| 24 | + return await fetchCache(url, { |
| 25 | + cacheKey, |
| 26 | + parser(data) { |
| 27 | + const lines = data.split("\n"); |
| 28 | + |
| 29 | + const sequences: EmojiSequence[] = []; |
| 30 | + |
| 31 | + for (let line of lines) { |
| 32 | + // skip empty line & comments |
| 33 | + if (line.trim() === "" || line.startsWith("#")) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + // remove line comment |
| 38 | + const commentIndex = line.indexOf("#"); |
| 39 | + if (commentIndex !== -1) { |
| 40 | + line = line.slice(0, commentIndex).trim(); |
| 41 | + } |
| 42 | + |
| 43 | + const [hex, property, description] = line.split(";").map((col) => col.trim()).slice(0, 4); |
| 44 | + |
| 45 | + if (hex == null || property == null || description == null) { |
| 46 | + throw new Error(`invalid line: ${line}`); |
| 47 | + } |
| 48 | + |
| 49 | + const expandedHex = expandHexRange(hex); |
| 50 | + |
| 51 | + for (const hex of expandedHex) { |
| 52 | + sequences.push({ |
| 53 | + hex: hex.replace(/\s+/g, "-"), |
| 54 | + property, |
| 55 | + description, |
| 56 | + gender: hex.includes(FEMALE_SIGN) ? "female" : hex.includes(MALE_SIGN) ? "male" : null, |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return sequences; |
| 62 | + }, |
| 63 | + bypassCache: ctx.force, |
| 64 | + }); |
| 65 | + })); |
| 66 | + |
| 67 | + return { |
| 68 | + sequences: sequences || [], |
| 69 | + zwj: zwj || [], |
| 70 | + }; |
| 71 | + }, |
| 72 | + async emojis(ctx) { |
| 73 | + const unicodeNames = await this.unicodeNames!(ctx); |
| 74 | + // const { sequences, zwj } = await this.sequences!(ctx); |
| 75 | + // const metadata = await this.metadata!(ctx); |
| 76 | + // const variations = await this.variations!(ctx); |
| 77 | + |
| 78 | + const emojis: Record<string, Record<string, Record<string, Emoji>>> = {}; |
| 79 | + |
| 80 | + const emojiData = await fetchCache(`https://unicode.org/Public/${ctx.emojiVersion === "13.1" ? "13.0" : ctx.emojiVersion}.0/ucd/emoji/emoji-data.txt`, { |
| 81 | + cacheKey: `v${ctx.emojiVersion}/emoji-data.json`, |
| 82 | + parser(data) { |
| 83 | + const lines = data.split("\n"); |
| 84 | + |
| 85 | + const emojiData: Record<string, EmojiData> = {}; |
| 86 | + |
| 87 | + for (const line of lines) { |
| 88 | + // skip empty line & comments |
| 89 | + if (line.trim() === "" || line.startsWith("#")) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + const lineCommentIndex = line.indexOf("#"); |
| 94 | + const lineComment = lineCommentIndex !== -1 ? line.slice(lineCommentIndex + 1).trim() : ""; |
| 95 | + |
| 96 | + let [hex, property] = line.split(";").map((col) => col.trim()).slice(0, 4); |
| 97 | + |
| 98 | + if (hex == null || property == null) { |
| 99 | + throw new Error(`invalid line: ${line}`); |
| 100 | + } |
| 101 | + |
| 102 | + // remove line comment from property |
| 103 | + const propertyCommentIndex = property.indexOf("#"); |
| 104 | + if (propertyCommentIndex !== -1) { |
| 105 | + property = property.slice(0, propertyCommentIndex).trim(); |
| 106 | + } |
| 107 | + |
| 108 | + if (property === "Extended_Pictographic") { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + const expandedHex = expandHexRange(hex); |
| 113 | + const emojiVersion = extractEmojiVersion(lineComment); |
| 114 | + |
| 115 | + const emoji: EmojiData = { |
| 116 | + description: lineComment, |
| 117 | + hexcode: "", |
| 118 | + gender: null, |
| 119 | + properties: [(property as Property) || "Emoji"], |
| 120 | + unicodeVersion: extractUnicodeVersion(emojiVersion, ctx.unicodeVersion), |
| 121 | + emojiVersion, |
| 122 | + name: unicodeNames[hex] || "", |
| 123 | + }; |
| 124 | + |
| 125 | + for (const hex of expandedHex) { |
| 126 | + if (emojiData[hex] != null) { |
| 127 | + emojiData[hex].properties = [...new Set([...emojiData[hex].properties, ...emoji.properties])]; |
| 128 | + } else { |
| 129 | + emojiData[hex] = { |
| 130 | + ...emoji, |
| 131 | + hexcode: hex.replace(/\s+/g, "-"), |
| 132 | + }; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return emojiData; |
| 138 | + }, |
| 139 | + bypassCache: ctx.force, |
| 140 | + }); |
| 141 | + |
| 142 | + return { |
| 143 | + emojiData, |
| 144 | + emojis, |
| 145 | + }; |
| 146 | + }, |
| 147 | + variations: async (ctx) => { |
| 148 | + return fetchCache(`https://unicode.org/Public/${ctx.emojiVersion === "13.1" ? "13.0" : ctx.emojiVersion}.0/ucd/emoji/emoji-variation-sequences.txt`, { |
| 149 | + cacheKey: `v${ctx.emojiVersion}/variations.json`, |
| 150 | + parser(data) { |
| 151 | + const lines = data.split("\n"); |
| 152 | + |
| 153 | + const variations: EmojiVariation[] = []; |
| 154 | + |
| 155 | + for (let line of lines) { |
| 156 | + // skip empty line & comments |
| 157 | + if (line.trim() === "" || line.startsWith("#")) { |
| 158 | + continue; |
| 159 | + } |
| 160 | + |
| 161 | + // remove line comment |
| 162 | + const commentIndex = line.indexOf("#"); |
| 163 | + if (commentIndex !== -1) { |
| 164 | + line = line.slice(0, commentIndex).trim(); |
| 165 | + } |
| 166 | + |
| 167 | + const [hex, style] = line.split(";").map((col) => col.trim()).slice(0, 4); |
| 168 | + |
| 169 | + if (hex == null || style == null) { |
| 170 | + throw new Error(`invalid line: ${line}`); |
| 171 | + } |
| 172 | + |
| 173 | + const hexcode = hex.replace(/\s+/g, "-"); |
| 174 | + |
| 175 | + const type = style.replace("style", "").trim(); |
| 176 | + |
| 177 | + if (type !== "text" && type !== "emoji") { |
| 178 | + throw new Error(`invalid style: ${style}`); |
| 179 | + } |
| 180 | + |
| 181 | + variations.push({ |
| 182 | + emoji: type === "emoji" ? hexcode : null, |
| 183 | + text: type === "text" ? hexcode : null, |
| 184 | + property: ["Emoji"], |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + return variations; |
| 189 | + }, |
| 190 | + bypassCache: ctx.force, |
| 191 | + }); |
| 192 | + }, |
8 | 193 | });
|
0 commit comments