mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-08 06:51:43 -04:00
(ts) convert src/buildPronoun.ts
This commit is contained in:
parent
bd332e3949
commit
1a12dce4a8
@ -28,7 +28,7 @@
|
||||
|
||||
<script>
|
||||
import { pronouns } from '../src/data.js';
|
||||
import { getPronoun } from '../src/buildPronoun.js';
|
||||
import { getPronoun } from '../src/buildPronoun.ts';
|
||||
|
||||
export default {
|
||||
props: {
|
||||
|
@ -48,7 +48,7 @@
|
||||
|
||||
<script>
|
||||
import { pronouns } from '../../../src/data.js';
|
||||
import { getPronoun } from '../../../src/buildPronoun.js';
|
||||
import { getPronoun } from '../../../src/buildPronoun.ts';
|
||||
import { buildDict } from '../../../src/helpers.ts';
|
||||
|
||||
export default {
|
||||
|
@ -55,7 +55,7 @@
|
||||
|
||||
<script>
|
||||
import { pronouns } from '../../../src/data.js';
|
||||
import { getPronoun } from '../../../src/buildPronoun.js';
|
||||
import { getPronoun } from '../../../src/buildPronoun.ts';
|
||||
import { buildDict } from '../../../src/helpers.ts';
|
||||
|
||||
export default {
|
||||
|
@ -55,7 +55,7 @@
|
||||
|
||||
<script>
|
||||
import { pronouns } from '../../../src/data.js';
|
||||
import { getPronoun } from '../../../src/buildPronoun.js';
|
||||
import { getPronoun } from '../../../src/buildPronoun.ts';
|
||||
import { buildDict } from '../../../src/helpers.ts';
|
||||
|
||||
export default {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { pronounLibrary, pronouns } from '../src/data.js';
|
||||
import { buildPronoun } from '../src/buildPronoun.js';
|
||||
import { buildPronoun } from '../src/buildPronoun.ts';
|
||||
import opinions from '../src/opinions.js';
|
||||
|
||||
export default {
|
||||
|
@ -354,7 +354,7 @@
|
||||
<script>
|
||||
import { head, buildList, buildDict, isValidLink } from '../src/helpers.ts';
|
||||
import { pronouns, pronounLibrary } from '../src/data.js';
|
||||
import { buildPronoun } from '../src/buildPronoun.js';
|
||||
import { buildPronoun } from '../src/buildPronoun.ts';
|
||||
import link from '../plugins/link.js';
|
||||
import { minBirthdate, maxBirthdate, formatDate, parseDate } from '../src/birthdate.js';
|
||||
import opinions from '../src/opinions.js';
|
||||
|
@ -116,7 +116,7 @@
|
||||
|
||||
<script>
|
||||
import { examples, pronouns, pronounLibrary } from '../src/data.js';
|
||||
import { buildPronoun } from '../src/buildPronoun.js';
|
||||
import { buildPronoun } from '../src/buildPronoun.ts';
|
||||
import { head } from '../src/helpers.ts';
|
||||
import GrammarTables from '../data/pronouns/GrammarTables.vue';
|
||||
import LinkedText from '../components/LinkedText.vue';
|
||||
|
@ -3,7 +3,7 @@ import SQL from 'sql-template-strings';
|
||||
import { createCanvas, loadImage } from 'canvas';
|
||||
import { loadSuml, loadSumlFromBase } from '../loader.ts';
|
||||
import avatar from '../avatar.ts';
|
||||
import { buildPronoun, parsePronouns } from '../../src/buildPronoun.js';
|
||||
import { buildPronoun, parsePronouns } from '../../src/buildPronoun.ts';
|
||||
import { loadTsv } from '../../src/tsv.js';
|
||||
import { handleErrorAsync } from '../../src/helpers.ts';
|
||||
import { Translator } from '../../src/translator.js';
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Router } from 'express';
|
||||
import { loadSuml, loadSumlFromBase, loadTsv } from '../loader.ts';
|
||||
import { buildPronoun, parsePronouns } from '../../src/buildPronoun.js';
|
||||
import { buildPronoun, parsePronouns } from '../../src/buildPronoun.ts';
|
||||
import { buildList, handleErrorAsync } from '../../src/helpers.ts';
|
||||
import { Example } from '../../src/classes.ts';
|
||||
import { caches } from '../../src/cache.js';
|
||||
|
@ -2,29 +2,28 @@ import { Pronoun } from './classes.ts';
|
||||
import Compressor from './compressor.js';
|
||||
import { buildDict, isEmoji, unescapeControlSymbols } from './helpers.ts';
|
||||
import MORPHEMES from '../data/pronouns/morphemes.js';
|
||||
import type { Config, NullPronounsConfig } from '../locale/config.ts';
|
||||
import type { Translator } from './translator.js';
|
||||
|
||||
export const addAliasesToPronouns = (pronouns) => {
|
||||
const pronounsWithAliases = {};
|
||||
for (const base in pronouns) {
|
||||
if (pronouns.hasOwnProperty(base)) {
|
||||
const pronoun = pronouns[base];
|
||||
pronounsWithAliases[base] = pronoun;
|
||||
for (const alias of pronoun.aliases) {
|
||||
pronounsWithAliases[alias] = pronoun;
|
||||
}
|
||||
export const addAliasesToPronouns = (pronouns: Record<string, Pronoun>): Record<string, Pronoun> => {
|
||||
const pronounsWithAliases: Record<string, Pronoun> = {};
|
||||
for (const [base, pronoun] of Object.entries(pronouns)) {
|
||||
pronounsWithAliases[base] = pronoun;
|
||||
for (const alias of pronoun.aliases) {
|
||||
pronounsWithAliases[alias] = pronoun;
|
||||
}
|
||||
}
|
||||
return pronounsWithAliases;
|
||||
};
|
||||
|
||||
export const getPronoun = (pronouns, id) => {
|
||||
export const getPronoun = (pronouns: Record<string, Pronoun>, id: string): Pronoun | undefined => {
|
||||
return addAliasesToPronouns(pronouns)[id];
|
||||
};
|
||||
|
||||
const conditionalKeyPlaceHolder = /#\/([^/]+)\/(\w+)?#/;
|
||||
const unconditionalKeyPlaceholder = /#/g;
|
||||
|
||||
const buildMorphemeFromTemplate = (key, template) => {
|
||||
const buildMorphemeFromTemplate = (key: string, template: string): string => {
|
||||
const variants = template.split('|');
|
||||
for (const variant of variants) {
|
||||
const conditionalMatch = variant.match(conditionalKeyPlaceHolder);
|
||||
@ -36,9 +35,20 @@ const buildMorphemeFromTemplate = (key, template) => {
|
||||
return variant.replace(unconditionalKeyPlaceholder, key);
|
||||
}
|
||||
}
|
||||
return template;
|
||||
};
|
||||
|
||||
const buildPronounFromTemplate = (key, template) => {
|
||||
interface PronounTemplate {
|
||||
description: string;
|
||||
normative?: boolean;
|
||||
morphemes: Record<string, string>;
|
||||
plural?: boolean;
|
||||
pluralHonorific?: boolean;
|
||||
aliases?: string[];
|
||||
history?: string;
|
||||
}
|
||||
|
||||
const buildPronounFromTemplate = (key: string, template: PronounTemplate): Pronoun => {
|
||||
return new Pronoun(
|
||||
key,
|
||||
template.description,
|
||||
@ -56,12 +66,12 @@ const buildPronounFromTemplate = (key, template) => {
|
||||
);
|
||||
};
|
||||
|
||||
const isModifier = (chunk, key, translator) => {
|
||||
const isModifier = (chunk: string, key: string, translator: Translator): boolean => {
|
||||
// use both locale and base translations to ensure backwards compatibility if key gets translated
|
||||
return chunk === `:${translator.translate(key)}` || chunk === `:${translator.get(key, false, true)}`;
|
||||
};
|
||||
|
||||
const extractModifierValue = (chunk, key, translator) => {
|
||||
const extractModifierValue = (chunk: string, key: string, translator: Translator): string | null => {
|
||||
// use both locale and base translations to ensure backwards compatibility if key gets translated
|
||||
const prefixes = [`:${translator.translate(key)}=`, `:${translator.get(key, false, true)}=`];
|
||||
for (const prefix of prefixes) {
|
||||
@ -72,12 +82,12 @@ const extractModifierValue = (chunk, key, translator) => {
|
||||
return null;
|
||||
};
|
||||
|
||||
const buildPronounFromSlashes = (config, path, translator) => {
|
||||
const buildPronounFromSlashes = (config: Config, path: string, translator: Translator): Pronoun | null => {
|
||||
const chunks = path.split(/(?<!`)\//);
|
||||
let plural = false;
|
||||
let pluralHonorific = false;
|
||||
let description = '';
|
||||
const morphemeChunks = [];
|
||||
const morphemeChunks: (string | null)[] = [];
|
||||
for (const chunk of chunks) {
|
||||
if (chunk.startsWith(':')) {
|
||||
if (config.pronouns.plurals && isModifier(chunk, 'pronouns.slashes.plural', translator)) {
|
||||
@ -89,7 +99,7 @@ const buildPronounFromSlashes = (config, path, translator) => {
|
||||
const descriptionModifierValue =
|
||||
extractModifierValue(chunk, 'pronouns.slashes.description', translator);
|
||||
if (descriptionModifierValue) {
|
||||
description = unescapeControlSymbols(descriptionModifierValue);
|
||||
description = unescapeControlSymbols(descriptionModifierValue)!;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -126,15 +136,19 @@ const buildPronounFromSlashes = (config, path, translator) => {
|
||||
false,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export const buildPronoun = (pronouns, path, translator) => {
|
||||
if (!path) {
|
||||
export const buildPronoun = (
|
||||
pronouns: Record<string, Pronoun>,
|
||||
path: string,
|
||||
translator: Translator,
|
||||
): Pronoun | null => {
|
||||
const config = global.config || process.env.CONFIG;
|
||||
if (!path || !config.pronouns.enabled) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const config = global.config || process.env.CONFIG;
|
||||
|
||||
for (const prefix of config.pronouns.sentence ? config.pronouns.sentence.prefixes : []) {
|
||||
if (`/${path}`.startsWith(`${prefix}/`)) {
|
||||
path = path.substring(prefix.length);
|
||||
@ -159,7 +173,7 @@ export const buildPronoun = (pronouns, path, translator) => {
|
||||
const pronounStrLen = pronounStr.map((x) => x.startsWith('!') ? parseInt(x.substring(1)) : 1).reduce((c, a) => c + a, 0);
|
||||
if (config.locale === 'pl' && baseArray && pronounStrLen < 31) {
|
||||
baseArray.splice(baseArray.length - 10, 1);
|
||||
if (pronounStr < 30) {
|
||||
if (pronounStrLen < 30) {
|
||||
baseArray = [
|
||||
...baseArray.slice(0, 4),
|
||||
baseArray[5],
|
||||
@ -183,16 +197,13 @@ export const buildPronoun = (pronouns, path, translator) => {
|
||||
? base
|
||||
: Pronoun.from(Compressor.uncompress(pronounStr, baseArray, config.locale));
|
||||
|
||||
if (!config) {
|
||||
return pronoun;
|
||||
}
|
||||
|
||||
if (!pronoun && config.pronouns.emoji !== false && isEmoji(path)) {
|
||||
pronoun = buildPronounFromTemplate(path, config.pronouns.emoji);
|
||||
}
|
||||
|
||||
if (!pronoun && config.pronouns.null && config.pronouns.null.morphemes && path.startsWith(':') && path.length < 12) {
|
||||
pronoun = buildPronounFromTemplate(path.substring(1), config.pronouns.null);
|
||||
const template = config.pronouns.null as NullPronounsConfig & { morphemes: Record<string, string> };
|
||||
pronoun = buildPronounFromTemplate(path.substring(1), template);
|
||||
}
|
||||
|
||||
if (!pronoun && config.pronouns.slashes !== false) {
|
||||
@ -202,7 +213,7 @@ export const buildPronoun = (pronouns, path, translator) => {
|
||||
return pronoun;
|
||||
};
|
||||
|
||||
export const parsePronouns = (pronounsRaw) => {
|
||||
export const parsePronouns = (pronounsRaw: Record<string, any>[]): Record<string, Pronoun> => {
|
||||
return buildDict(function* () {
|
||||
for (const t of pronounsRaw) {
|
||||
const aliases = t.key.replace(/،/g, ',').split(',');
|
@ -1,6 +1,6 @@
|
||||
import { Example, NounTemplate, PronounGroup, PronounLibrary, Person, NounDeclension } from './classes.ts';
|
||||
import { buildDict, buildList } from './helpers.ts';
|
||||
import { parsePronouns } from './buildPronoun.js';
|
||||
import { parsePronouns } from './buildPronoun.ts';
|
||||
|
||||
import pronounsRaw from '../data/pronouns/pronouns.tsv';
|
||||
export const pronouns = parsePronouns(pronounsRaw);
|
||||
|
@ -21,7 +21,7 @@ jest.unstable_mockModule('../data/pronouns/morphemes.js', () => {
|
||||
});
|
||||
|
||||
const { Pronoun } = await import('../src/classes.ts');
|
||||
const { buildPronoun } = await import('../src/buildPronoun.js');
|
||||
const { buildPronoun } = await import('../src/buildPronoun.ts');
|
||||
const { default: pronouns, generated: generatedPronouns } = await import('./fixtures/pronouns.ts');
|
||||
|
||||
beforeEach(() => {
|
||||
@ -135,7 +135,7 @@ describe('when configured that null pronouns are available', () => {
|
||||
));
|
||||
});
|
||||
test('builds nothing if name too long', () => {
|
||||
expect(buildPronoun(pronouns, ':Abcdefghijklmnopqrstuvwxyz', translator)).toBeUndefined();
|
||||
expect(buildPronoun(pronouns, ':Abcdefghijklmnopqrstuvwxyz', translator)).toBeNull();
|
||||
});
|
||||
|
||||
describe('with conditional placeholders', () => {
|
||||
@ -144,10 +144,10 @@ describe('when configured that null pronouns are available', () => {
|
||||
});
|
||||
|
||||
test.each(['S', 'Ringelnatz', 'Max', 'X'])('builds morpheme conditionally if name matches', (name) => {
|
||||
expect(buildPronoun(pronouns, `:${name}`, translator).morphemes.possessive_pronoun).toBe(`${name}’`);
|
||||
expect(buildPronoun(pronouns, `:${name}`, translator)!.morphemes.possessive_pronoun).toBe(`${name}’`);
|
||||
});
|
||||
test.each(['Sofi', 'Xavier'])('builds morpheme by default if name does not match', (name) => {
|
||||
expect(buildPronoun(pronouns, `:${name}`, translator).morphemes.possessive_pronoun).toBe(`${name}s`);
|
||||
expect(buildPronoun(pronouns, `:${name}`, translator)!.morphemes.possessive_pronoun).toBe(`${name}s`);
|
||||
});
|
||||
});
|
||||
|
||||
@ -244,10 +244,10 @@ describe('when configured that slashes contain all morphemes', () => {
|
||||
actual.toEqual(generatedPronouns.aerWithUnsetPossessivePronoun);
|
||||
});
|
||||
test('builds nothing if morphemes are missing', () => {
|
||||
expect(buildPronoun(pronouns, 'ae/aer/aer/aerself', translator)).toBeUndefined();
|
||||
expect(buildPronoun(pronouns, 'ae/aer/aer/aerself', translator)).toBeNull();
|
||||
});
|
||||
test('builds nothing if too many morphemes are given', () => {
|
||||
expect(buildPronoun(pronouns, 'ae/aer/aer/aers/aerself/aer', translator)).toBeUndefined();
|
||||
expect(buildPronoun(pronouns, 'ae/aer/aer/aers/aerself/aer', translator)).toBeNull();
|
||||
});
|
||||
test('builds nothing if description too long', () => {
|
||||
expect(buildPronoun(
|
||||
@ -268,10 +268,10 @@ describe('when configured that slashes contain some morphemes', () => {
|
||||
actual.toEqual(generatedPronouns.aerWithUnsetPossessivePronoun);
|
||||
});
|
||||
test('builds nothing if morphemes are missing', () => {
|
||||
expect(buildPronoun(pronouns, 'ae/aer/aer', translator)).toBeUndefined();
|
||||
expect(buildPronoun(pronouns, 'ae/aer/aer', translator)).toBeNull();
|
||||
});
|
||||
test('builds nothing if too many morphemes are given', () => {
|
||||
expect(buildPronoun(pronouns, 'ae/aer/aer/aers/aerself', translator)).toBeUndefined();
|
||||
expect(buildPronoun(pronouns, 'ae/aer/aer/aers/aerself', translator)).toBeNull();
|
||||
});
|
||||
});
|
||||
describe('when configured that slashes cannot contain morphemes', () => {
|
||||
@ -321,13 +321,13 @@ describe('building generated pronouns from commas', () => {
|
||||
));
|
||||
});
|
||||
test('fails when too few parts are given', () => {
|
||||
expect(buildPronoun(pronouns, 'ae, translator,aer,aer,aers,aerself', translator)).toBeUndefined();
|
||||
expect(buildPronoun(pronouns, 'ae, translator,aer,aer,aers,aerself', translator)).toBeNull();
|
||||
});
|
||||
test('fails when many few parts are given', () => {
|
||||
expect(buildPronoun(pronouns, 'ae, translator,aer,aer,aers,aerself,aersing,0,', translator)).toBeUndefined();
|
||||
expect(buildPronoun(pronouns, 'ae, translator,aer,aer,aers,aerself,aersing,0,', translator)).toBeNull();
|
||||
});
|
||||
test('fails when base pronoun is unknown', () => {
|
||||
expect(buildPronoun(pronouns, 's/he, translator,!2,aers,!2,', translator)).toBeUndefined();
|
||||
expect(buildPronoun(pronouns, 's/he, translator,!2,aers,!2,', translator)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user