Skip to content

Commit 29f9e54

Browse files
authored
Merge pull request #41 from mojisdev/add-handlers
feat: add more handlers
2 parents 1dbbc89 + 5d630fc commit 29f9e54

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2017
-785
lines changed

.changeset/true-taxis-march.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@mojis/internal-utils": patch
3+
"@mojis/adapters": patch
4+
"@mojis/parsers": patch
5+
"@mojis/cli": patch
6+
---
7+
8+
feat: improve handlers

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"turbo": "catalog:",
3131
"typescript": "catalog:",
3232
"vitest": "catalog:",
33-
"vitest-fetch-mock": "catalog:"
33+
"msw": "catalog:"
3434
},
3535
"pnpm": {
3636
"onlyBuiltDependencies": [

packages/adapters/eslint.config.js

-11
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@ import { luxass } from "@luxass/eslint-config";
33

44
export default luxass({
55
type: "lib",
6-
}, {
7-
files: ["!**/*.test.ts"],
8-
rules: {
9-
"no-restricted-globals": [
10-
"error",
11-
{
12-
name: "fetchMock",
13-
message: "using fetchMock in non-test files is not allowed",
14-
},
15-
],
16-
},
176
}, {
187
ignores: ["**/*.md"],
198
});

packages/adapters/src/handler.ts

-106
This file was deleted.

packages/adapters/src/_handlers.ts packages/adapters/src/handlers.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { modernMetadataHandler } from "./handlers/modern/metadata";
1+
import { baseMetadataHandler, notSupportedMetadataHandler } from "./handlers/metadata";
22
import { modernSequenceHandler } from "./handlers/modern/sequence";
3-
import { modernVariationHandler } from "./handlers/modern/variation";
4-
import { preAlignmentMetadataHandler } from "./handlers/pre-alignment/metadata";
3+
import { baseVariationHandler, notSupportedVariationHandler } from "./handlers/variation";
54

65
export const METADATA_HANDLERS = [
7-
modernMetadataHandler,
8-
preAlignmentMetadataHandler,
6+
baseMetadataHandler,
7+
notSupportedMetadataHandler,
98
];
109

1110
export const SEQUENCE_HANDLERS = [
1211
modernSequenceHandler,
1312
];
1413

1514
export const VARIATION_HANDLERS = [
16-
modernVariationHandler,
15+
baseVariationHandler,
16+
notSupportedVariationHandler,
1717
];
1818

1919
export const ALL_HANDLERS = {

packages/adapters/src/handlers/modern/metadata.ts packages/adapters/src/handlers/metadata.ts

+44-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { EmojiGroup, EmojiMetadata } from "@mojis/internal-utils";
2-
import { extractEmojiVersion, extractUnicodeVersion } from "@mojis/internal-utils";
3-
import { defineAdapterHandler } from "../../define";
2+
import { extractEmojiVersion, extractUnicodeVersion, isBefore } from "@mojis/internal-utils";
3+
import { defineAdapterHandler } from "../define";
44

55
function slugify(val: string): string {
66
return val.normalize("NFD")
@@ -14,19 +14,19 @@ function slugify(val: string): string {
1414
}
1515

1616
// These emoji versions doesn't seem to have a emoji-test,
17-
// where we can extract the metadata from.
17+
// which we need to extract the groups and such from.
18+
// We will probably just have to "generate" them from a html page.
1819
const DISALLOWED_EMOJI_VERSIONS = ["1.0", "2.0", "3.0"];
1920

20-
export const modernMetadataHandler = defineAdapterHandler({
21+
export const baseMetadataHandler = defineAdapterHandler({
2122
type: "metadata",
2223
shouldExecute: (ctx) => {
24+
// Since we can't get the metadata for these versions,
25+
// we will just skip them.
26+
// We have a fallback handler for these versions (notSupportedMetadataHandler) find it below.
2327
return !DISALLOWED_EMOJI_VERSIONS.includes(ctx.emoji_version);
2428
},
2529
urls: (ctx) => {
26-
if (DISALLOWED_EMOJI_VERSIONS.includes(ctx.emoji_version)) {
27-
return undefined;
28-
}
29-
3030
return {
3131
url: `https://unicode.org/Public/emoji/${ctx.emoji_version}/emoji-test.txt`,
3232
cacheKey: `v${ctx.emoji_version}/metadata`,
@@ -91,8 +91,18 @@ export const modernMetadataHandler = defineAdapterHandler({
9191
const hexcode = baseHexcode.trim().replace(/\s+/g, "-");
9292
const qualifier = baseQualifier.trim();
9393

94-
const emojiVersion = extractEmojiVersion(comment.trim());
95-
const [emoji, trimmedComment] = comment.trim().split(` E${emojiVersion} `);
94+
// if the emoji_version is v5 and under.
95+
// the content after the # doesn't include the emoji version.
96+
let emoji;
97+
let trimmedComment;
98+
99+
const extractedEmojiVersion = extractEmojiVersion(comment.trim());
100+
101+
if (isBefore(ctx.emoji_version, "6.0.0")) {
102+
[emoji, trimmedComment] = comment.trim().split(" ");
103+
} else {
104+
[emoji, trimmedComment] = comment.trim().split(` E${extractedEmojiVersion} `);
105+
}
96106

97107
const groupName = currentGroup?.slug ?? "unknown";
98108
const subgroupName = currentGroup?.subgroups[currentGroup.subgroups.length - 1] ?? "unknown";
@@ -107,8 +117,8 @@ export const modernMetadataHandler = defineAdapterHandler({
107117
group: groupName,
108118
subgroup: subgroupName,
109119
qualifier,
110-
emojiVersion: emojiVersion || null,
111-
unicodeVersion: extractUnicodeVersion(emojiVersion, ctx.unicode_version),
120+
emojiVersion: extractedEmojiVersion || null,
121+
unicodeVersion: extractUnicodeVersion(extractedEmojiVersion, ctx.unicode_version),
112122
description: trimmedComment || "",
113123
emoji: emoji || null,
114124
hexcodes: hexcode.split("-"),
@@ -124,3 +134,25 @@ export const modernMetadataHandler = defineAdapterHandler({
124134
return transformed;
125135
},
126136
});
137+
138+
// Handles the versions that doesn't seem to have an emoji-test file.
139+
// We will just return an empty object for these versions.
140+
export const notSupportedMetadataHandler = defineAdapterHandler({
141+
type: "metadata",
142+
shouldExecute: (ctx) => {
143+
return DISALLOWED_EMOJI_VERSIONS.includes(ctx.emoji_version);
144+
},
145+
urls: () => {
146+
return undefined;
147+
},
148+
parser: "generic",
149+
transform() {
150+
return undefined;
151+
},
152+
output() {
153+
return {
154+
groups: [],
155+
emojis: {},
156+
};
157+
},
158+
});

packages/adapters/src/handlers/modern/sequence.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import { defineAdapterHandler } from "../../define";
55

66
export const modernSequenceHandler = defineAdapterHandler({
77
type: "sequence",
8-
urls: (ctx) => {
8+
urls: ({ emoji_version }) => {
99
return [
1010
{
1111
key: "sequences",
12-
url: `https://unicode.org/Public/emoji/${ctx.emoji_version}/emoji-sequences.txt`,
13-
cacheKey: `v${ctx.emoji_version}/sequences`,
12+
url: `https://unicode.org/Public/emoji/${emoji_version}/emoji-sequences.txt`,
13+
cacheKey: `v${emoji_version}/sequences`,
1414
},
1515
{
1616
key: "zwj",
17-
url: `https://unicode.org/Public/emoji/${ctx.emoji_version}/emoji-zwj-sequences.txt`,
18-
cacheKey: `v${ctx.emoji_version}/zwj-sequences`,
17+
url: `https://unicode.org/Public/emoji/${emoji_version}/emoji-zwj-sequences.txt`,
18+
cacheKey: `v${emoji_version}/zwj-sequences`,
1919
},
2020
];
2121
},

packages/adapters/src/handlers/modern/variation.ts

-33
This file was deleted.

packages/adapters/src/handlers/pre-alignment/metadata.ts

-22
This file was deleted.

0 commit comments

Comments
 (0)