Fix filteredArray logic

This commit is contained in:
Alex Gleason 2023-03-13 16:37:40 -05:00
parent d0ceac9987
commit e9ae8d2c45
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -4,10 +4,13 @@ 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) {
return z.any().array().transform((arr) => (
arr.map((item) => schema.safeParse(item).success ? item as z.infer<T> : undefined)
.filter((item): item is z.infer<T> => Boolean(item))
));
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))
));
}
/** Map a list of CustomEmoji to their shortcodes. */