aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/deinflector.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-09-18 19:50:32 -0700
committerAlex Yatskov <alex@foosoft.net>2016-09-18 19:50:32 -0700
commitb44d19b35e120133a4bb751e176aec4d14b6cbc5 (patch)
tree05b23ef7e70e57f2cf38584a20c864ff0d7e4853 /ext/bg/js/deinflector.js
parent7bd642e53868e0a3154b285be969cc2e6068f46f (diff)
parent4e2d7327c5b1aa8eed035dc4c177a3b9b49c5ab7 (diff)
Merge branch 'dev'
Diffstat (limited to 'ext/bg/js/deinflector.js')
-rw-r--r--ext/bg/js/deinflector.js56
1 files changed, 33 insertions, 23 deletions
diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js
index 0eabd0f3..8b9f88e2 100644
--- a/ext/bg/js/deinflector.js
+++ b/ext/bg/js/deinflector.js
@@ -26,32 +26,36 @@ class Deinflection {
}
validate(validator) {
- for (const tags of validator(this.term)) {
- if (this.tags.length === 0) {
- return true;
- }
-
- for (const tag of this.tags) {
- if (tags.indexOf(tag) !== -1) {
+ return validator(this.term).then(sets => {
+ for (const tags of sets) {
+ if (this.tags.length === 0) {
return true;
}
+
+ for (const tag of this.tags) {
+ if (tags.includes(tag)) {
+ return true;
+ }
+ }
}
- }
- return false;
+ return false;
+ });
}
deinflect(validator, rules) {
- if (this.validate(validator)) {
- const child = new Deinflection(this.term, this.tags);
- this.children.push(child);
- }
+ const promises = [
+ this.validate(validator).then(valid => {
+ const child = new Deinflection(this.term, this.tags);
+ this.children.push(child);
+ })
+ ];
for (const rule in rules) {
for (const variant of rules[rule]) {
let allowed = this.tags.length === 0;
for (const tag of this.tags) {
- if (variant.ti.indexOf(tag) !== -1) {
+ if (variant.ti.includes(tag)) {
allowed = true;
break;
}
@@ -62,14 +66,24 @@ class Deinflection {
}
const term = this.term.slice(0, -variant.ki.length) + variant.ko;
- const child = new Deinflection(term, variant.to, rule);
- if (child.deinflect(validator, rules)) {
- this.children.push(child);
+ if (term.length === 0) {
+ continue;
}
+
+ const child = new Deinflection(term, variant.to, rule);
+ promises.push(
+ child.deinflect(validator, rules).then(valid => {
+ if (valid) {
+ this.children.push(child);
+ }
+ }
+ ));
}
}
- return this.children.length > 0;
+ return Promise.all(promises).then(() => {
+ return this.children.length > 0;
+ });
}
gather() {
@@ -105,10 +119,6 @@ class Deinflector {
deinflect(term, validator) {
const node = new Deinflection(term);
- if (node.deinflect(validator, this.rules)) {
- return node.gather();
- }
-
- return null;
+ return node.deinflect(validator, this.rules).then(success => success ? node.gather() : []);
}
}