Skip to content

Commit f54d358

Browse files
committed
feat: enhance createLoom with presets and add emoji test flags
- Updated the createLoom function to support custom presets, allowing for more flexible input configurations. - Introduced a new JSON file for emoji test flags, integrating it into the emoji test generator. - Refactored the emoji test generator to utilize the new presets structure for better organization and maintainability.
1 parent a133983 commit f54d358

File tree

4 files changed

+72
-137
lines changed

4 files changed

+72
-137
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[
2+
{
3+
"group": "Flags",
4+
"subgroups": [
5+
{
6+
"subgroup": "country-flag",
7+
"entries": [
8+
{
9+
"codePoints": ["1F1E6", "1F1E8"],
10+
"status": "fully-qualified",
11+
"comment": "flag: Argentina"
12+
},
13+
{
14+
"codePoints": ["1F1E6", "1F1F4"],
15+
"status": "fully-qualified",
16+
"comment": "flag: Angola"
17+
},
18+
{
19+
"codePoints": ["1F1E6", "1F1F6"],
20+
"status": "fully-qualified",
21+
"comment": "flag: Antarctica"
22+
}
23+
]
24+
}
25+
]
26+
}
27+
]

src/generators/emoji-test.ts

Lines changed: 21 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import { z } from "zod";
2-
import bundledSmileys from "../bundled-json-files/emoji-test-smileys.json";
2+
import bundledFlags from "../bundled-json-files/emoji-test-flags.json" with { type: "json" };
3+
import bundledSmileys from "../bundled-json-files/emoji-test-smileys.json" with { type: "json" };
34
import { createLoom } from "../loom";
45

6+
const VALID_STATUS = [
7+
"component",
8+
"fully-qualified",
9+
"minimally-qualified",
10+
"unqualified",
11+
] as const;
12+
513
const entry = z.object({
614
codePoints: z.array(z.string()),
7-
status: z.union([
8-
z.literal("component"),
9-
z.literal("fully-qualified"),
10-
z.literal("minimally-qualified"),
11-
z.literal("unqualified"),
12-
]),
15+
// can't use union of literals since typescript can only infer
16+
// the status type to a string, and not the union of literals
17+
status: z.string().refine(
18+
(val) => VALID_STATUS.includes(val as (typeof VALID_STATUS)[number]),
19+
{
20+
message: `status must be one of: ${VALID_STATUS.join(", ")}`,
21+
},
22+
),
1323
comment: z.string(),
1424
});
1525

@@ -27,7 +37,7 @@ const emojiTestOptionsSchema = z.object({
2737
version: z.string(),
2838
});
2939

30-
const baseEmojiTest = createLoom({
40+
export const emojiTest = createLoom({
3141
inputSchema: emojiTestInputSchema,
3242
optionsSchema: emojiTestOptionsSchema,
3343
predicate: (ctx) => {
@@ -48,129 +58,8 @@ const baseEmojiTest = createLoom({
4858

4959
return template;
5060
},
51-
});
52-
53-
export const emojiTest = Object.assign(baseEmojiTest, {
54-
smileys: (version: string): string => {
55-
return baseEmojiTest({
56-
version,
57-
separator: ";",
58-
commentPrefix: "#",
59-
// @ts-expect-error asd
60-
input: bundledSmileys,
61-
});
62-
},
63-
skinTone: (version: string): string => {
64-
return baseEmojiTest({
65-
version,
66-
separator: ";",
67-
commentPrefix: "#",
68-
input: [
69-
{
70-
group: "Component",
71-
subgroups: [
72-
{
73-
subgroup: "skin-tone",
74-
entries: [
75-
{
76-
codePoints: ["1F3FB"],
77-
status: "component",
78-
comment: "light skin tone",
79-
},
80-
{
81-
codePoints: ["1F3FC"],
82-
status: "component",
83-
comment: "medium-light skin tone",
84-
},
85-
{
86-
codePoints: ["1F3FD"],
87-
status: "component",
88-
comment: "medium skin tone",
89-
},
90-
{
91-
codePoints: ["1F3FE"],
92-
status: "component",
93-
comment: "medium-dark skin tone",
94-
},
95-
{
96-
codePoints: ["1F3FF"],
97-
status: "component",
98-
comment: "dark skin tone",
99-
},
100-
],
101-
},
102-
],
103-
},
104-
],
105-
});
106-
},
107-
family: (version: string): string => {
108-
return baseEmojiTest({
109-
version,
110-
separator: ";",
111-
commentPrefix: "#",
112-
input: [
113-
{
114-
group: "People & Body",
115-
subgroups: [
116-
{
117-
subgroup: "family",
118-
entries: [
119-
{
120-
codePoints: ["1F468", "200D", "1F469", "200D", "1F467"],
121-
status: "fully-qualified",
122-
comment: "family: man, woman, girl",
123-
},
124-
{
125-
codePoints: ["1F468", "200D", "1F469", "200D", "1F467", "200D", "1F466"],
126-
status: "fully-qualified",
127-
comment: "family: man, woman, girl, boy",
128-
},
129-
{
130-
codePoints: ["1F468", "200D", "1F469", "200D", "1F466", "200D", "1F466"],
131-
status: "fully-qualified",
132-
comment: "family: man, woman, boy, boy",
133-
},
134-
],
135-
},
136-
],
137-
},
138-
],
139-
});
140-
},
141-
142-
flag: (version: string): string => {
143-
return baseEmojiTest({
144-
version,
145-
separator: ";",
146-
commentPrefix: "#",
147-
input: [
148-
{
149-
group: "Flags",
150-
subgroups: [
151-
{
152-
subgroup: "country-flag",
153-
entries: [
154-
{
155-
codePoints: ["1F1E6", "1F1E8"],
156-
status: "fully-qualified",
157-
comment: "flag: Argentina",
158-
},
159-
{
160-
codePoints: ["1F1E6", "1F1F4"],
161-
status: "fully-qualified",
162-
comment: "flag: Angola",
163-
},
164-
{
165-
codePoints: ["1F1E6", "1F1F6"],
166-
status: "fully-qualified",
167-
comment: "flag: Antarctica",
168-
},
169-
],
170-
},
171-
],
172-
},
173-
],
174-
});
61+
presets: {
62+
smileys: bundledSmileys,
63+
flags: bundledFlags,
17564
},
17665
});

src/loom.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { z } from "zod";
55
export function createLoom<
66
TInputSchema extends z.ZodType,
77
TOptionsSchema extends z.ZodType,
8-
>(config: LoomConfig<TInputSchema, TOptionsSchema>): LoomInstance<TInputSchema, TOptionsSchema> {
9-
return (options) => {
8+
TPresets extends Record<string, TInputSchema["_input"][]>,
9+
>(config: LoomConfig<TInputSchema, TOptionsSchema, TPresets>): LoomInstance<TInputSchema, TOptionsSchema, TPresets> {
10+
const base = (options: TOptionsSchema["_input"] & { input: TInputSchema["_input"][] }): string => {
1011
// validate options against schema
1112
const validatedOptions = config.optionsSchema.parse(options);
1213

@@ -36,6 +37,15 @@ export function createLoom<
3637

3738
return lines.join("\n");
3839
};
40+
41+
const extras = Object.entries(config.presets ?? {}).reduce((acc, [key, value]) => {
42+
acc[key] = (options: TOptionsSchema["_input"]) => {
43+
return base({ ...options, input: value });
44+
};
45+
return acc;
46+
}, {} as Record<string, (options: TOptionsSchema["_input"]) => string>);
47+
48+
return Object.assign(base, extras) as LoomInstance<TInputSchema, TOptionsSchema, TPresets>;
3949
}
4050

4151
function buildLoomContext<TOptionsSchema extends z.ZodType>(options: TOptionsSchema["_input"]): LoomContext<TOptionsSchema> {

src/types.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface LoomContext<TOptionsSchema extends z.ZodType> extends LoomConte
3939
export interface LoomConfig<
4040
TInputSchema extends z.ZodType,
4141
TOptionsSchema extends z.ZodType,
42+
TPresets extends Record<string, TInputSchema["_input"][]>,
4243
> {
4344
/**
4445
* The schema of the input data.
@@ -67,11 +68,19 @@ export interface LoomConfig<
6768
* The predicate function. If provided, the loom will only process items that return true.
6869
*/
6970
predicate?: (ctx: LoomContext<TOptionsSchema>, item: TInputSchema["_input"]) => boolean;
71+
72+
/**
73+
* Attach custom presets to the loom.
74+
*/
75+
presets?: TPresets;
7076
}
7177

72-
export interface LoomInstance<
78+
export type LoomInstance<
7379
TInputSchema extends z.ZodType,
7480
TOptionsSchema extends z.ZodType,
75-
> {
81+
TPresets extends Record<string, TInputSchema["_input"][]>,
82+
> = {
7683
(options: TOptionsSchema["_input"] & { input: TInputSchema["_input"][] }): string;
77-
}
84+
} & {
85+
[key in keyof TPresets]: (options: TOptionsSchema["_input"]) => string;
86+
};

0 commit comments

Comments
 (0)