mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-08-03 11:07:00 -04:00
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import type { H3Event } from 'h3';
|
|
|
|
import type { PronounExamplesData } from '~/locale/data.ts';
|
|
import { getLocale, loadPronounExamples } from '~/server/data.ts';
|
|
import { PronounExample } from '~/src/classes.ts';
|
|
import type { Pronoun } from '~/src/classes.ts';
|
|
import { buildList } from '~/src/helpers.ts';
|
|
import { Example } from '~/src/language/examples.ts';
|
|
|
|
export interface PublicPronoun extends Omit<Pronoun, 'config' | 'hidden' | 'name'> {
|
|
examples: string[];
|
|
name: string;
|
|
}
|
|
|
|
const buildExample = (e: PronounExamplesData): PronounExample => new PronounExample(
|
|
Example.parse(e.singular),
|
|
Example.parse(e.plural || e.singular),
|
|
Example.parse(e.null || e.singular),
|
|
Example.parse(e.null_plural || e.plural || e.singular),
|
|
e.isHonorific,
|
|
);
|
|
|
|
export const getPronounExamples = async (event: H3Event): Promise<PronounExamplesData[]> => {
|
|
const examples = getQuery(event).examples as string[] | undefined;
|
|
|
|
if (!examples || !examples.length) {
|
|
return loadPronounExamples(getLocale(event));
|
|
}
|
|
|
|
return examples.map((example) => {
|
|
const [singular, plural, isHonorific] = example.split('|');
|
|
return { singular, plural, isHonorific: !!isHonorific };
|
|
});
|
|
};
|
|
|
|
export const addExamples = (pronoun: Pronoun, examples: PronounExamplesData[]): string[] => {
|
|
return buildList(function* () {
|
|
for (const example of examples) {
|
|
yield buildExample(example).example(pronoun).toSpellingString(pronoun.toExampleValues());
|
|
}
|
|
});
|
|
};
|
|
|
|
type IntermediatePronoun = Partial<Omit<Pronoun, 'name'>> & { examples?: string[]; name: string };
|
|
|
|
export const makePublicPronoun = (pronoun: Pronoun, examples: PronounExamplesData[]): PublicPronoun => {
|
|
const publicPronoun = pronoun.clone() as unknown as IntermediatePronoun;
|
|
publicPronoun.examples = addExamples(pronoun, examples);
|
|
publicPronoun.name = pronoun.name();
|
|
delete publicPronoun.config;
|
|
delete publicPronoun.hidden;
|
|
return publicPronoun as PublicPronoun;
|
|
};
|