This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/src/buildTemplate.js

66 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-07-26 03:59:49 -07:00
import {MORPHEMES, Template} from "./classes";
2020-07-22 13:19:23 -07:00
import Compressor from "./compressor";
2020-07-26 03:59:49 -07:00
import {buildDict} from "./helpers";
2020-07-22 13:19:23 -07:00
2020-07-26 03:59:49 -07:00
export const addAliasesToTemplates = (templates) => {
const templatesWithAliases = {}
for (let base in templates) {
if (templates.hasOwnProperty(base)) {
const template = templates[base];
templatesWithAliases[base] = template;
for (let alias of template.aliases) {
templatesWithAliases[alias] = template;
}
2020-07-24 07:26:03 -07:00
}
}
2020-07-26 03:59:49 -07:00
return templatesWithAliases;
2020-07-24 07:26:03 -07:00
}
2020-07-26 03:59:49 -07:00
export const getTemplate = (templates, id) => {
return addAliasesToTemplates(templates)[id];
2020-07-24 09:50:33 -07:00
}
2020-07-26 03:59:49 -07:00
export const buildTemplate = (templates, path) => {
const templatesWithAliases = addAliasesToTemplates(templates);
2020-07-22 13:19:23 -07:00
const templateStr = path.split(',');
2020-07-26 04:14:25 -07:00
let base = null;
for (let option of templateStr[0].split('&')) {
if (!base) {
base = templatesWithAliases[option]
} else {
base = base.merge(templatesWithAliases[option])
}
}
2020-07-22 13:19:23 -07:00
return templateStr.length === 1
2020-07-26 04:14:25 -07:00
? base
2020-07-22 13:19:23 -07:00
: Template.from(Compressor.uncompress(templateStr, base ? base.toArray() : null));
}
2020-07-26 03:59:49 -07:00
export const parseTemplates = (templatesRaw) => {
return buildDict(function* () {
for (let t of templatesRaw) {
const aliases = t.key.split(',');
yield [
aliases[0],
new Template(
t.description,
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,
)
];
}
});
}