@dcoffey/espells
Version:
Pure JS/TS spellchecker, using Hunspell dictionaries. Based on Spylls.
64 lines • 2.39 kB
JavaScript
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { concat } from "../util.js";
import { LKWord } from "./lk-word.js";
/**
* Represents a hypothesis of how a word may be represented as a
* {@link Prefix}, stem, and {@link Suffix}. A word always has a full text
* and stem, but may optionally have up to two prefixes and suffixes.
* Instances with no actual affixes are valid, as well.
*/
export class AffixForm {
constructor(text, stem, { prefix, suffix, prefix2, suffix2, inDictionary } = {}) {
if (text instanceof LKWord) {
this.text = text.word;
this.stem = stem ?? text.word;
}
else {
this.text = text;
this.stem = stem ?? text;
}
this.prefix = prefix;
this.suffix = suffix;
this.prefix2 = prefix2;
this.suffix2 = suffix2;
this.inDictionary = inDictionary;
}
/**
* Returns a new {@link AffixForm}, cloned from this current instance, but
* with any properties given replaced.
*/
replace(opts) {
return new AffixForm(opts.text ?? this.text, opts.stem ?? this.stem, {
prefix: opts.prefix ?? this.prefix,
suffix: opts.suffix ?? this.suffix,
prefix2: opts.prefix2 ?? this.prefix2,
suffix2: opts.suffix2 ?? this.suffix2,
inDictionary: opts.inDictionary ?? this.inDictionary
});
}
/** True if the form has any affixes. */
get hasAffixes() {
return Boolean(this.suffix || this.prefix);
}
/** The complete set of flags this form has. */
get flags() {
let flags = this.inDictionary?.flags ?? new Set();
if (this.prefix)
flags = concat(flags, this.prefix.flags);
if (this.suffix)
flags = concat(flags, this.suffix.flags);
return flags;
}
/** Returns every {@link Prefix} and {@link Suffix} this form has. */
affixes() {
return [this.prefix2, this.prefix, this.suffix, this.suffix2].filter(affix => Boolean(affix));
}
has(flag) {
if (!this.hasAffixes)
return false;
return this.affixes().every(affix => affix.has(flag));
}
}
//# sourceMappingURL=forms.js.map