2020-11-10 15:47:44 -08:00
|
|
|
import { Pronoun } from "./classes";
|
|
|
|
import Compressor from "./compressor";
|
2020-11-15 05:13:14 -08:00
|
|
|
import {buildDict, isEmoji} from "./helpers";
|
2020-11-10 15:47:44 -08:00
|
|
|
import MORPHEMES from '../data/pronouns/morphemes';
|
|
|
|
|
|
|
|
export const addAliasesToPronouns = (pronouns) => {
|
|
|
|
const pronounsWithAliases = {}
|
|
|
|
for (let base in pronouns) {
|
|
|
|
if (pronouns.hasOwnProperty(base)) {
|
|
|
|
const pronoun = pronouns[base];
|
|
|
|
pronounsWithAliases[base] = pronoun;
|
|
|
|
for (let alias of pronoun.aliases) {
|
|
|
|
pronounsWithAliases[alias] = pronoun;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pronounsWithAliases;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getPronoun = (pronouns, id) => {
|
|
|
|
return addAliasesToPronouns(pronouns)[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const buildPronoun = (pronouns, path) => {
|
|
|
|
const pronounsWithAliases = addAliasesToPronouns(pronouns);
|
|
|
|
|
|
|
|
const pronounStr = path.split(',');
|
|
|
|
|
|
|
|
let base = null;
|
|
|
|
for (let option of pronounStr[0].split('&')) {
|
|
|
|
if (!base) {
|
|
|
|
base = pronounsWithAliases[option]
|
|
|
|
} else {
|
|
|
|
base = base.merge(pronounsWithAliases[option])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:13:14 -08:00
|
|
|
let pronoun = pronounStr.length === 1
|
2020-11-10 15:47:44 -08:00
|
|
|
? base
|
|
|
|
: Pronoun.from(Compressor.uncompress(pronounStr, base ? base.toArray() : null));
|
2020-11-15 05:13:14 -08:00
|
|
|
|
2020-11-15 06:55:44 -08:00
|
|
|
if (!process.env.CONFIG) {
|
|
|
|
return pronoun;
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:13:14 -08:00
|
|
|
if (!pronoun && process.env.CONFIG.pronouns.emoji !== false && isEmoji(path)) {
|
|
|
|
pronoun = buildPronounFromTemplate(path, process.env.CONFIG.pronouns.emoji);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pronoun && process.env.CONFIG.pronouns.null !== false && path.startsWith(':') && path.length < 12) {
|
|
|
|
pronoun = buildPronounFromTemplate(path.substring(1), process.env.CONFIG.pronouns.null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pronoun;
|
2020-11-10 15:47:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const parsePronouns = (pronounsRaw) => {
|
|
|
|
return buildDict(function* () {
|
|
|
|
for (let t of pronounsRaw) {
|
|
|
|
const aliases = t.key.split(',');
|
|
|
|
|
|
|
|
yield [
|
|
|
|
aliases[0],
|
|
|
|
new Pronoun(
|
|
|
|
aliases[0],
|
|
|
|
t.description,
|
|
|
|
t.normative,
|
|
|
|
buildDict(function* () {
|
|
|
|
for (let morpheme of MORPHEMES) {
|
|
|
|
yield [morpheme, t[morpheme]];
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[t.plural],
|
|
|
|
[t.pluralHonorific],
|
|
|
|
t.sources ? t.sources.split(',') : [],
|
|
|
|
aliases.slice(1),
|
|
|
|
t.history,
|
2020-11-28 07:52:48 -08:00
|
|
|
t.pronounceable,
|
2020-11-10 15:47:44 -08:00
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-11-15 05:13:14 -08:00
|
|
|
|
|
|
|
export const buildPronounFromTemplate = (key, template) => {
|
|
|
|
return new Pronoun(
|
|
|
|
key,
|
|
|
|
template.description,
|
|
|
|
template.normative || false,
|
|
|
|
buildDict(function*(morphemes) {
|
|
|
|
for (let k in morphemes) {
|
|
|
|
if (morphemes.hasOwnProperty(k)) {
|
|
|
|
yield [k, morphemes[k].replace(/#/g, key)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, template.morphemes),
|
2020-11-28 07:52:48 -08:00
|
|
|
[template.plural || false],
|
|
|
|
[template.pluralHonorific || false],
|
2020-11-15 05:13:14 -08:00
|
|
|
template.sources || [],
|
|
|
|
template.aliases || [],
|
|
|
|
template.history || null,
|
2020-11-28 07:52:48 -08:00
|
|
|
false,
|
2020-11-15 05:13:14 -08:00
|
|
|
)
|
|
|
|
}
|