pleroma/app/soapbox/schemas/utils.ts

24 lines
779 B
TypeScript
Raw Normal View History

2023-03-10 10:42:49 -08:00
import z from 'zod';
import type { CustomEmoji } from './custom-emoji';
/** Validates individual items in an array, dropping any that aren't valid. */
function filteredArray<T extends z.ZodTypeAny>(schema: T) {
2023-03-13 14:37:40 -07:00
return z.any().array()
.transform((arr) => (
arr.map((item) => {
const parsed = schema.safeParse(item);
return parsed.success ? parsed.data : undefined;
}).filter((item): item is z.infer<T> => Boolean(item))
));
2023-03-10 10:42:49 -08:00
}
/** Map a list of CustomEmoji to their shortcodes. */
function makeCustomEmojiMap(customEmojis: CustomEmoji[]) {
return customEmojis.reduce<Record<string, CustomEmoji>>((result, emoji) => {
result[`:${emoji.shortcode}:`] = emoji;
return result;
}, {});
}
export { filteredArray, makeCustomEmojiMap };