#19 formy wymienne - base

This commit is contained in:
Andrea Vos 2020-07-26 13:14:25 +02:00
parent 81a78e4186
commit a410f8f5fc
3 changed files with 69 additions and 8 deletions

View File

@ -14,7 +14,10 @@
</h2>
<p class="h6 small text-center mb-0 mt-2" v-if="selectedTemplate.description">
<em>
({{selectedTemplate.description}})
({{Array.isArray(selectedTemplate.description)
? ('Formy wymienne: ' + selectedTemplate.description.join(' lub '))
: selectedTemplate.description
}})
</em>
</p>
</div>
@ -29,7 +32,7 @@
<ul>
<li v-for="example in examples" class="my-1">
<span v-for="part in example[(example.isHonorific ? selectedTemplate.pluralHonorific : selectedTemplate.plural) ? 'pluralParts' : 'singularParts']">
<strong v-if="part.variable">{{selectedTemplate.morphemes[part.str]}}</strong>
<strong v-if="part.variable">{{selectedTemplate.getMorpheme(part.str, counter)}}</strong>
<span v-else>{{part.str}}</span>
</span>
</li>
@ -75,6 +78,13 @@
getTemplate: getTemplate,
selectedTemplate: buildTemplate(templates, this.$route.path.substr(1)),
counter: 0,
}
},
mounted() {
if (process.client) {
setInterval(_ => this.counter++, 1000);
}
},
head() {

View File

@ -24,10 +24,18 @@ export const buildTemplate = (templates, path) => {
const templatesWithAliases = addAliasesToTemplates(templates);
const templateStr = path.split(',');
const base = templatesWithAliases[templateStr[0]]
let base = null;
for (let option of templateStr[0].split('&')) {
if (!base) {
base = templatesWithAliases[option]
} else {
base = base.merge(templatesWithAliases[option])
}
}
return templateStr.length === 1
? templatesWithAliases[templateStr[0]]
? base
: Template.from(Compressor.uncompress(templateStr, base ? base.toArray() : null));
}

View File

@ -1,3 +1,5 @@
import {buildDict} from "./helpers";
export class ExamplePart {
constructor(variable, str) {
this.variable = variable;
@ -106,14 +108,16 @@ export const MORPHEMES = [
];
const escape = s => {
if (Array.isArray(s)) {
s = s.join('&');
}
return (s || '')
.replace(/,/g, '')
.replace(/!/g, '')
.replace(/\./g, '')
//.replace(/\/', '%2F')
.replace(/#/g, '%23')
.replace(/\?/g, '%3F')
.replace(/&/g, '%26');
.replace(/\?/g, '%3F');
}
export class Template {
@ -131,8 +135,19 @@ export class Template {
return this.morphemes['pronoun_n'];
}
name() {
return this.morphemes['pronoun_n'] + '/' + this.morphemes['pronoun_g'];
nameOptions() {
const options = new Set();
const optionsN = this.morphemes.pronoun_n.split('&');
const optionsG = this.morphemes.pronoun_g.split('&');
for (let i in optionsN) {
options.add(optionsN[i] + '/' + optionsG[i < optionsG.length - 1 ? i : optionsG.length - 1]);
}
return [...options]
}
name(glue = ' lub ') {
return this.nameOptions().join(glue)
}
clone() {
@ -143,6 +158,34 @@ export class Template {
return this.toString() === other.toString();
}
merge(other) {
if (this.plural !== other.plural || this.pluralHonorific !== other.pluralHonorific) {
// Cannot mix plurality
return null;
}
return new Template(
Array.isArray(this.description) ? [...this.description, other.description] : [this.description, other.description],
buildDict(function* (that, other) {
for (let morpheme of MORPHEMES) {
yield [morpheme, (that.morphemes[morpheme] || '') + '&' + (other.morphemes[morpheme] || '')]
}
}, this, other),
this.plural,
this.pluralHonorific,
);
}
getMorpheme(morpheme, counter = 0) {
if (!this.morphemes[morpheme]) {
return null;
}
const options = this.morphemes[morpheme].split('&');
return options[counter % options.length]
}
toArray() {
return [
...Object.values(this.morphemes).map(s => escape(s)),