peppa/packages/shared/src/wording.ts
2026-11-15 18:05:34 +05:00

76 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// PEP-123: the words a person reads depend on the view (PEP-119). «Продакт» reads «задача» and
// «агент» (docs/glossary.md); «Инженер» keeps «стори» (one OpenSpec change; «задачи» there are
// the items of tasks.md) and «раннер» (the process, as opposed to the agent's persona).
// Pure module: used by the browser too.
export type WordMode = "product" | "engineer";
/** Case forms: singular nom/gen/dat/acc/ins/pre and the same in plural. */
export type WordForm = "nom" | "gen" | "dat" | "acc" | "ins" | "pre" | "pl" | "genPl" | "datPl" | "accPl" | "insPl" | "prePl";
export const BRAND = "Peppa FACTORY";
const TASK: Record<WordForm, string> = {
nom: "задача",
gen: "задачи",
dat: "задаче",
acc: "задачу",
ins: "задачей",
pre: "задаче",
pl: "задачи",
genPl: "задач",
datPl: "задачам",
accPl: "задачи",
insPl: "задачами",
prePl: "задачах",
};
// «агент» and «раннер» decline alike; both are animate, so the accusative follows the genitive.
const ENDING: Record<WordForm, string> = { nom: "", gen: "а", dat: "у", acc: "а", ins: "ом", pre: "е", pl: "ы", genPl: "ов", datPl: "ам", accPl: "ов", insPl: "ами", prePl: "ах" };
const cap = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
/** «1 задача», «2 задачи», «5 задач» — the form of a counted noun. */
export function plural(n: number, one: string, few: string, many: string): string {
const d = Math.abs(n) % 100;
const u = d % 10;
if (d > 10 && d < 20) return many;
if (u === 1) return one;
return u >= 2 && u <= 4 ? few : many;
}
export interface Words {
mode: WordMode;
/** True in «Продакт»: hide what only an engineer reads. */
product: boolean;
engineer: boolean;
/** «задача» / «стори» in the given form. */
task(form?: WordForm): string;
/** The same, capitalized. */
Task(form?: WordForm): string;
/** «5 задач» / «5 стори». */
tasks(n: number): string;
/** «агент» / «раннер» in the given form. */
runner(form?: WordForm): string;
Runner(form?: WordForm): string;
}
export function wording(mode: WordMode | null | undefined): Words {
const product = mode !== "engineer";
const task = (form: WordForm = "nom") => (product ? TASK[form] : "стори");
const runner = (form: WordForm = "nom") => (product ? "агент" : "раннер") + ENDING[form];
return {
mode: product ? "product" : "engineer",
product,
engineer: !product,
task,
Task: (form) => cap(task(form)),
tasks: (n) => `${n} ${product ? plural(n, TASK.nom, TASK.gen, TASK.genPl) : "стори"}`,
runner,
Runner: (form) => cap(runner(form)),
};
}
/** Texts stored or sent without knowing the reader's view (chat events, Telegram, push). */
export const PRODUCT_WORDS = wording("product");