mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-24 05:05:20 -04:00

the #shared alias used by Nuxt cannot be easily disabled and to prevent breackage with jiti, we make use of it
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
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<string, MorphemeValue | undefined>;
|
|
|
|
constructor(values: Record<string, MorphemeValue | string | undefined>) {
|
|
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;
|
|
}
|
|
}
|