mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-08-03 11:07:00 -04:00
(refactor)(nouns) move cases.ts into nounsData.suml
This commit is contained in:
parent
dc1ee7424e
commit
71499f3f9a
@ -33,20 +33,24 @@ const declension = computed(() => {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const declension = nounsData.declensions[template.value.declension];
|
||||
if (!declension[numerus.value]) {
|
||||
const declension = nounsData.declensions?.[template.value.declension];
|
||||
if (declension?.[numerus.value] === undefined || nounsData.classExample?.[numerus.value] === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const entries = Object.entries(declension[numerus.value] ?? {}).map(([caseAbbreviation, declensionSuffix]) => {
|
||||
return [caseAbbreviation.toUpperCase() + (props.plural ? '_pl' : ''),
|
||||
nounsData.classExample[numerus.value][caseAbbreviation].replace(/\{([^}]+)}/, (_match, morpheme) => {
|
||||
const article = nounsData.classExample?.[numerus.value][caseAbbreviation]
|
||||
.replace(/\{([^}]+)}/, (_match, morpheme) => {
|
||||
const value = props.nounConvention.morphemes[morpheme];
|
||||
if (value === undefined) {
|
||||
return '';
|
||||
}
|
||||
return typeof value === 'string' ? value : value.spelling;
|
||||
}) + stem.value + template.value.suffix + declensionSuffix];
|
||||
}) ?? '';
|
||||
return [
|
||||
caseAbbreviation.toUpperCase() + (props.plural ? '_pl' : ''),
|
||||
article + stem.value + template.value.suffix + declensionSuffix,
|
||||
];
|
||||
});
|
||||
|
||||
return new NounDeclension(Object.fromEntries(entries));
|
||||
|
@ -36,12 +36,12 @@ const examples = computed(() => {
|
||||
if (exampleValues.value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
return nounsData?.examples.map((example) => Example.parse(example))
|
||||
return nounsData.examples?.map((example) => Example.parse(example))
|
||||
.filter((example) => example.areRequiredExampleValuesPresent(exampleValues.value!));
|
||||
});
|
||||
|
||||
const nounConventionGroup = computed(() => {
|
||||
return Object.values(nounsData.groups).find((nounConventionGroup) => {
|
||||
return Object.values(nounsData.groups ?? []).find((nounConventionGroup) => {
|
||||
return nounConventionGroup.conventions.includes(props.nounConvention.key);
|
||||
});
|
||||
});
|
||||
|
@ -5,7 +5,7 @@ const nounsData = await loadNounsData();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ul v-if="nounsData" class="list-group mt-4">
|
||||
<ul v-if="nounsData.groups" class="list-group mt-4">
|
||||
<li
|
||||
v-for="nounConventionGroup of withKey(nounsData.groups)"
|
||||
:key="nounConventionGroup.key"
|
||||
|
@ -11,7 +11,7 @@ if (nounsData === undefined) {
|
||||
throw new Error('nounsData is undefined');
|
||||
}
|
||||
const visibleNounConventions = computed(() => {
|
||||
return withKey(nounsData.conventions).filter((nounConvention) => {
|
||||
return withKey(nounsData.conventions ?? {}).filter((nounConvention) => {
|
||||
return props.nounConventionGroup.conventions.includes(nounConvention.key);
|
||||
});
|
||||
});
|
||||
|
@ -24,10 +24,10 @@ const stem = computed(() => {
|
||||
return stems[template.stem ?? 'default'];
|
||||
});
|
||||
|
||||
const declension = nounsData.declensions[template.declension];
|
||||
const declension = nounsData.declensions?.[template.declension];
|
||||
|
||||
const singularExample = computed(() => {
|
||||
if (declension.singular?.n === undefined) {
|
||||
if (declension?.singular?.n === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ const singularExample = computed(() => {
|
||||
});
|
||||
|
||||
const pluralExample = computed(() => {
|
||||
if (declension.plural?.n === undefined) {
|
||||
if (declension?.plural?.n === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import type { NounDeclension } from '~/src/classes.ts';
|
||||
import { loadNounDeclensionTemplates, loadNounCases } from '~/src/data.ts';
|
||||
import { loadNounDeclensionTemplates, loadNounsData } from '~/src/data.ts';
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
word: string;
|
||||
@ -14,7 +14,7 @@ const props = withDefaults(defineProps<{
|
||||
plural: false,
|
||||
});
|
||||
|
||||
const [nounDeclensionTemplates, cases] = await Promise.all([loadNounDeclensionTemplates(), loadNounCases()]);
|
||||
const [nounsData, nounDeclensionTemplates] = await Promise.all([loadNounsData(), loadNounDeclensionTemplates()]);
|
||||
|
||||
const visible = ref(props.open);
|
||||
|
||||
@ -68,7 +68,7 @@ const findTemplate = (): NounDeclension | null => {
|
||||
>
|
||||
<strong>
|
||||
{{ caseName }}
|
||||
<small v-if="!condense">({{ cases[caseName] }})</small>
|
||||
<small v-if="!condense">({{ nounsData.cases?.[caseName.toLowerCase()] }})</small>
|
||||
</strong>
|
||||
{{ declined.join(' / ') }}
|
||||
</li>
|
||||
|
@ -1,6 +0,0 @@
|
||||
export default {
|
||||
N: 'wer? was?',
|
||||
G: 'wessen?',
|
||||
D: 'wem?',
|
||||
A: 'wen? was?',
|
||||
};
|
@ -1,3 +1,9 @@
|
||||
cases:
|
||||
n: 'wer? was?'
|
||||
g: 'wessen?'
|
||||
d: 'wem?'
|
||||
a: 'wen? was?'
|
||||
|
||||
morphemes:
|
||||
- 'article_n'
|
||||
- 'article_g'
|
||||
|
@ -1,9 +0,0 @@
|
||||
export default {
|
||||
M: 'kto? co?',
|
||||
D: 'kogo? czego?',
|
||||
C: 'komu? czemu?',
|
||||
B: 'kogo? co?',
|
||||
N: 'z kim? z czym?',
|
||||
Msc: 'o kim? o czym?',
|
||||
W: 'o!',
|
||||
};
|
8
locale/pl/nouns/nounsData.suml
Normal file
8
locale/pl/nouns/nounsData.suml
Normal file
@ -0,0 +1,8 @@
|
||||
cases:
|
||||
m: 'kto? co?'
|
||||
d: 'kogo? czego?'
|
||||
c: 'komu? czemu?'
|
||||
b: 'kogo? co?'
|
||||
n: 'z kim? z czym?'
|
||||
msc: 'o kim? o czym?'
|
||||
w: 'o!'
|
@ -1,9 +0,0 @@
|
||||
export default {
|
||||
M: 'kto? co?',
|
||||
D: 'kogo? czego?',
|
||||
C: 'komu? czemu?',
|
||||
B: 'kogo? co?',
|
||||
N: 'z kim? z czym?',
|
||||
Msc: 'o kim? o czym?',
|
||||
W: 'o!',
|
||||
};
|
@ -1,9 +0,0 @@
|
||||
export default {
|
||||
M: 'kto? co?',
|
||||
D: 'kogo? czego?',
|
||||
C: 'komu? czemu?',
|
||||
B: 'kogo? co?',
|
||||
N: 'z kim? z czym?',
|
||||
Msc: 'o kim? o czym?',
|
||||
W: 'o!',
|
||||
};
|
@ -25,7 +25,7 @@ const findNounConvention = async (path: string, config: Config) => {
|
||||
|
||||
const nounsData = await loadNounsData();
|
||||
|
||||
return withKey(nounsData?.conventions ?? {})
|
||||
return withKey(nounsData.conventions ?? {})
|
||||
.find((nounConvention) => nounConvention.key === path);
|
||||
};
|
||||
|
||||
|
12
src/data.ts
12
src/data.ts
@ -64,11 +64,11 @@ export const loadPronounLibrary = async (config: Config) => {
|
||||
return new PronounLibrary(config, pronounGroups, pronouns);
|
||||
};
|
||||
|
||||
export const loadNounsData = async (): Promise<NounsData | undefined> => {
|
||||
export const loadNounsData = async (): Promise<NounsData> => {
|
||||
try {
|
||||
return (await import(`~/locale/${getLocale()}/nouns/nounsData.suml`)).default;
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
@ -109,14 +109,6 @@ export const loadNounAbbreviations = async (): Promise<Record<string, string>> =
|
||||
}
|
||||
};
|
||||
|
||||
export const loadNounCases = async () => {
|
||||
try {
|
||||
return (await import(`~/locale/${getLocale()}/nouns/cases.ts`)).default;
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
export const loadPeople = async () => {
|
||||
const peopleRaw = (await import(`~/locale/${getLocale()}/people/people.tsv`)).default;
|
||||
return buildList(function* () {
|
||||
|
17
src/nouns.ts
17
src/nouns.ts
@ -68,12 +68,13 @@ export interface NounDeclension {
|
||||
}
|
||||
|
||||
export interface NounsData {
|
||||
morphemes: string[];
|
||||
examples: string[];
|
||||
grammarTables: GrammarTableDefinition[];
|
||||
classes: Record<string, NounClass>;
|
||||
classExample: NounClassExample;
|
||||
declensions: Record<string, NounDeclension>;
|
||||
groups: Record<string, NounConventionGroup>;
|
||||
conventions: Record<string, NounConvention>;
|
||||
cases?: Record<string, string>;
|
||||
morphemes?: string[];
|
||||
examples?: string[];
|
||||
grammarTables?: GrammarTableDefinition[];
|
||||
classes?: Record<string, NounClass>;
|
||||
classExample?: NounClassExample;
|
||||
declensions?: Record<string, NounDeclension>;
|
||||
groups?: Record<string, NounConventionGroup>;
|
||||
conventions?: Record<string, NounConvention>;
|
||||
}
|
||||
|
@ -163,14 +163,31 @@ describe.each(allLocales)('data files of $code', async ({ code }) => {
|
||||
});
|
||||
}
|
||||
|
||||
if (config.nouns.conventions?.enabled) {
|
||||
const nounsData = await loadSuml<NounsData>(`locale/${code}/nouns/nounsData.suml`);
|
||||
let nounsData: NounsData | undefined;
|
||||
try {
|
||||
nounsData = await loadSuml<NounsData>(`locale/${code}/nouns/nounsData.suml`);
|
||||
} catch (error) {
|
||||
nounsData = undefined;
|
||||
}
|
||||
|
||||
if (config.nouns.enabled && config.nouns.conventions?.enabled) {
|
||||
test('config.nouns.conventions.enabled requires properties in nouns/nounsData.suml', () => {
|
||||
expect(nounsData?.morphemes).toBeDefined();
|
||||
expect(nounsData?.conventions).toBeDefined();
|
||||
expect(nounsData?.groups).toBeDefined();
|
||||
expect(nounsData?.classes).toBeDefined();
|
||||
expect(nounsData?.classExample).toBeDefined();
|
||||
expect(nounsData?.examples).toBeDefined();
|
||||
});
|
||||
}
|
||||
|
||||
if (nounsData) {
|
||||
describe('nouns/nounsData.suml', () => {
|
||||
test('has valid variant types', async () => {
|
||||
if (nounsData.grammarTables) {
|
||||
test('grammar tables have valid variant types', async () => {
|
||||
const grammarTableVariantsConverter = (await import(`~/locale/${code}/language/grammarTableVariantsConverter.ts`)).default as VariantsFromBaseConverter;
|
||||
|
||||
for (const grammarTable of nounsData.grammarTables) {
|
||||
for (const grammarTable of nounsData.grammarTables!) {
|
||||
for (const section of grammarTable.sections) {
|
||||
if (!Array.isArray(section.variants)) {
|
||||
expect(Object.keys(grammarTableVariantsConverter)).toContain(section.variants.type);
|
||||
@ -178,17 +195,32 @@ describe.each(allLocales)('data files of $code', async ({ code }) => {
|
||||
}
|
||||
}
|
||||
});
|
||||
test('has valid morphemes', () => {
|
||||
for (const convention of Object.values(nounsData.conventions)) {
|
||||
}
|
||||
if (nounsData.conventions) {
|
||||
test('conventions have valid morphemes', () => {
|
||||
for (const convention of Object.values(nounsData.conventions!)) {
|
||||
expect(nounsData.morphemes)
|
||||
.toEqual(expect.arrayContaining(Object.keys(convention.morphemes)));
|
||||
}
|
||||
});
|
||||
test('groups reference conventions by key', () => {
|
||||
for (const group of Object.values(nounsData.groups)) {
|
||||
expect(Object.keys(nounsData.conventions)).toEqual(expect.arrayContaining(group.conventions));
|
||||
}
|
||||
if (nounsData.conventions && nounsData.groups) {
|
||||
test('convention groups reference conventions by key', () => {
|
||||
for (const group of Object.values(nounsData.groups!)) {
|
||||
expect(Object.keys(nounsData.conventions!)).toEqual(expect.arrayContaining(group.conventions));
|
||||
}
|
||||
});
|
||||
}
|
||||
if (nounsData.cases && nounsData.declensions) {
|
||||
test('declensions have valid cases', () => {
|
||||
for (const declension of Object.values(nounsData.declensions!)) {
|
||||
for (const numerus of ['singular', 'plural'] as const) {
|
||||
expect(Object.keys(nounsData.cases!))
|
||||
.toEqual(expect.arrayContaining(Object.keys(declension[numerus] ?? {})));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user