Skip to content
This repository was archived by the owner on Mar 1, 2025. It is now read-only.

Commit 0e145b9

Browse files
committed
feat: enhance emoji sequence handling by adding support for zwj sequences and improving data fetching logic
1 parent b4c5fd9 commit 0e145b9

File tree

4 files changed

+55
-35
lines changed

4 files changed

+55
-35
lines changed

src/adapter/base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default defineMojiAdapter({
1515
range: "*",
1616
groups: async ({ version, force }) => {
1717
if (version === "1.0" || version === "2.0" || version === "3.0") {
18+
console.warn(`version ${version} does not have group data`);
1819
return [];
1920
}
2021

src/adapter/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface BaseAdapterContext {
3636
}
3737

3838
export type GroupFn = (ctx: BaseAdapterContext) => Promise<EmojiGroup[]>;
39-
export type SequenceFn = (ctx: BaseAdapterContext) => Promise<EmojiSequence[]>;
39+
export type SequenceFn = (ctx: BaseAdapterContext) => Promise<{ zwj: EmojiSequence[]; sequences: EmojiSequence[] }>;
4040

4141
export const ADAPTERS = new Map<string, MojiAdapter>();
4242

src/adapter/v16.ts

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,62 @@ export default defineMojiAdapter({
1010
range: ">=16.0.0 <17.0.0",
1111
extend: "base",
1212
sequences: async (ctx) => {
13-
const sequences = await fetchCache(`https://unicode.org/Public/emoji/${ctx.version}/emoji-sequences.txt`, {
14-
cacheKey: `v${ctx.version}/sequences.json`,
15-
parser(data) {
16-
const lines = data.split("\n");
13+
const [sequences, zwj] = await Promise.all([
14+
{
15+
cacheKey: `v${ctx.version}/sequences.json`,
16+
url: `https://unicode.org/Public/emoji/${ctx.version}/emoji-sequences.txt`,
17+
},
18+
{
19+
cacheKey: `v${ctx.version}/zwj-sequences.json`,
20+
url: `https://unicode.org/Public/emoji/${ctx.version}/emoji-zwj-sequences.txt`,
21+
},
22+
].map(async ({ cacheKey, url }) => {
23+
return await fetchCache(url, {
24+
cacheKey,
25+
parser(data) {
26+
const lines = data.split("\n");
1727

18-
const sequences: EmojiSequence[] = [];
28+
const sequences: EmojiSequence[] = [];
1929

20-
for (let line of lines) {
30+
for (let line of lines) {
2131
// skip empty line & comments
22-
if (line.trim() === "" || line.startsWith("#")) {
23-
continue;
24-
}
32+
if (line.trim() === "" || line.startsWith("#")) {
33+
continue;
34+
}
2535

26-
// remove line comment
27-
const commentIndex = line.indexOf("#");
28-
if (commentIndex !== -1) {
29-
line = line.slice(0, commentIndex).trim();
30-
}
36+
// remove line comment
37+
const commentIndex = line.indexOf("#");
38+
if (commentIndex !== -1) {
39+
line = line.slice(0, commentIndex).trim();
40+
}
3141

32-
const [hex, property, description] = line.split(";").map((col) => col.trim()).slice(0, 4);
42+
const [hex, property, description] = line.split(";").map((col) => col.trim()).slice(0, 4);
3343

34-
if (hex == null || property == null || description == null) {
35-
throw new Error(`invalid line: ${line}`);
36-
}
44+
if (hex == null || property == null || description == null) {
45+
throw new Error(`invalid line: ${line}`);
46+
}
3747

38-
const expandedHex = expandHexRange(hex);
48+
const expandedHex = expandHexRange(hex);
3949

40-
for (const hex of expandedHex) {
41-
sequences.push({
42-
hex: hex.replace(/\s+/g, "-"),
43-
property,
44-
description,
45-
gender: hex === FEMALE_SIGN ? "female" : hex === MALE_SIGN ? "male" : null,
46-
});
50+
for (const hex of expandedHex) {
51+
sequences.push({
52+
hex: hex.replace(/\s+/g, "-"),
53+
property,
54+
description,
55+
gender: hex.includes(FEMALE_SIGN) ? "female" : hex.includes(MALE_SIGN) ? "male" : null,
56+
});
57+
}
4758
}
48-
}
4959

50-
return sequences;
51-
},
52-
bypassCache: ctx.force,
53-
});
60+
return sequences;
61+
},
62+
bypassCache: ctx.force,
63+
});
64+
}));
5465

55-
return sequences;
66+
return {
67+
sequences: sequences || [],
68+
zwj: zwj || [],
69+
};
5670
},
5771
});

src/cli.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,17 @@ cli.command(
5353
throw new Error(`no adapter found for version ${version}`);
5454
}
5555

56-
const groups = await adapter.sequences!({ version, force });
56+
const { sequences, zwj } = await adapter.sequences!({ version, force });
5757

5858
await fs.ensureDir(`./data/v${version}`);
59+
await fs.writeFile(
60+
`./data/v${version}/zwj-sequences.json`,
61+
JSON.stringify(zwj, null, 2),
62+
"utf-8",
63+
);
5964
return fs.writeFile(
6065
`./data/v${version}/sequences.json`,
61-
JSON.stringify(groups, null, 2),
66+
JSON.stringify(sequences, null, 2),
6267
"utf-8",
6368
);
6469
});

0 commit comments

Comments
 (0)