mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-23 04:34:15 -04:00

the #shared alias used by Nuxt cannot be easily disabled and to prevent breackage with jiti, we make use of it
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import type { H3Event } from 'h3';
|
|
|
|
import { PronounExample } from '#shared/classes.ts';
|
|
import type { Pronoun } from '#shared/classes.ts';
|
|
import { buildList } from '#shared/helpers.ts';
|
|
import { Example } from '#shared/language/examples.ts';
|
|
import type { PronounExamplesData } from '~~/locale/data.ts';
|
|
import { getLocale, loadPronounExamples } from '~~/server/data.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;
|
|
};
|