mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-24 21:46:22 -04:00
feat(rewrite): Implementation of /pronouns/:pronoun
route
This commit is contained in:
parent
0ff1a90f79
commit
017e669416
@ -87,7 +87,7 @@ export interface Pronoun {
|
||||
forms: Record<string, { written: string; pronounced: string }>;
|
||||
}
|
||||
|
||||
export interface Example {
|
||||
export interface PronounExample {
|
||||
singular?: string;
|
||||
plural?: string;
|
||||
isHonorific: boolean;
|
||||
@ -153,7 +153,7 @@ export class Locale {
|
||||
private _pronouns: Array<Pronoun> = [];
|
||||
private _pronounsByAlias: Record<string, number> = {};
|
||||
|
||||
private _examples: Array<Example> = []; // TODO: Type these properly
|
||||
private _examples: Array<PronounExample> = []; // TODO: Type these properly
|
||||
// private _morphemes: Array<string>;
|
||||
|
||||
public async load() {
|
||||
@ -231,7 +231,7 @@ export class Locale {
|
||||
singular: isNotBlank(example.singular) ? example.singular : undefined,
|
||||
plural: isNotBlank(example.plural) ? example.plural : undefined,
|
||||
isHonorific: example.isHonorific
|
||||
} satisfies Example)
|
||||
} satisfies PronounExample)
|
||||
}
|
||||
}
|
||||
|
||||
@ -263,7 +263,7 @@ export class Locale {
|
||||
}
|
||||
export function examplesFor(
|
||||
pronoun: Pronoun,
|
||||
examples: Array<Example>
|
||||
examples: Array<PronounExample>
|
||||
): Array<string> {
|
||||
const finished = [];
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { Example, examplesFor, getLocale, Pronoun } from "#self/locales";
|
||||
import { isNotBlank } from "@pronounspage/common/util";
|
||||
import { examplesFor, getLocale, Pronoun, PronounExample } from "#self/locales";
|
||||
import { isNotBlank, parseBool } from "@pronounspage/common/util";
|
||||
|
||||
export type V1AppInstance = AppInstance;
|
||||
|
||||
@ -56,7 +56,7 @@ export const routes = async function (app: AppInstance) {
|
||||
locale: Type.String(),
|
||||
});
|
||||
|
||||
function transformPronoun(pronoun: Pronoun, examples: Array<Example>) {
|
||||
function transformPronoun(pronoun: Pronoun, examples: Array<PronounExample>) {
|
||||
const morphemes: Record<string, string> = {};
|
||||
const pronunciations: Record<string, string> = {};
|
||||
for (const [name, form] of Object.entries(pronoun.forms)) {
|
||||
@ -106,6 +106,15 @@ export const routes = async function (app: AppInstance) {
|
||||
}
|
||||
);
|
||||
|
||||
function parseExample(s: string): PronounExample {
|
||||
const [singular, plural, isHonorific] = s.split('|');
|
||||
return {
|
||||
singular: isNotBlank(singular) ? singular : undefined,
|
||||
plural: isNotBlank(plural) ? plural : undefined,
|
||||
isHonorific: isNotBlank(isHonorific) && parseBool(isHonorific),
|
||||
};
|
||||
}
|
||||
|
||||
app.get(
|
||||
"/:locale/pronouns/*",
|
||||
{
|
||||
@ -133,7 +142,7 @@ export const routes = async function (app: AppInstance) {
|
||||
|
||||
const examples = req.query["examples[]"];
|
||||
|
||||
return transformPronoun(found, locale.examples);
|
||||
return transformPronoun(found, examples.length < 1 ? locale.examples : examples.map(parseExample));
|
||||
}
|
||||
);
|
||||
} satisfies AppPluginAsync;
|
||||
|
Loading…
x
Reference in New Issue
Block a user