import { capitalise } from '#shared/helpers.ts'; export const getBaseMorpheme = (morpheme: string): string => { return morpheme.replace(/^'/, ''); }; export const toMorphemeValue = (value: MorphemeValue | string): MorphemeValue => { if (typeof value === 'string') { return { spelling: value }; } return value; }; export interface MorphemeValue { /** * spelling (grapheme) */ spelling: string; /** * pronunciation (phoneme), can be: * `string` starting with `=`, meaning that it should be pronounced like a word part in the respective language * `string`, meaning it should be pronounced according to IPA * `undefined` or absent, meaning that it should be pronounced like `spelling` in the respective language * `false`, meaning it is unpronounceable */ pronunciation?: string | false; } export class MorphemeValues { private readonly values: Record; constructor(values: Record) { this.values = Object.fromEntries(Object.entries(values) .map(([morpheme, value]) => [morpheme, value !== undefined ? toMorphemeValue(value) : undefined])); } getSpelling(morpheme: string): string | undefined { let capital = false; if (morpheme.startsWith('\'')) { capital = true; morpheme = morpheme.substring(1); } const value = this.values[morpheme]; if (value === undefined) { return undefined; } return capital ? capitalise(value.spelling) : value.spelling; } getPronunciation(morpheme: string): string | false | undefined { return this.values[morpheme]?.pronunciation; } }