UNPKG

@dcoffey/espells

Version:

Pure JS/TS spellchecker, using Hunspell dictionaries. Based on Spylls.

65 lines 2.61 kB
/* 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/. */ /** * A pattern for checking if a pair of {@link AffixForm}s are a valid * compounding arrangement. Uses the following syntax: * * ```text * endchars[/flag] beginchars[/flag] [replacement] * ``` * * Basically, a pair of forms has to have its first word end with * `endchars`, and its second word to begin with `beginchars`. The flags * given follow a similar logic. * * `replacement` is a strange optional string, meaning "if `replacement` * can be found at the word boundary of the pair of forms, make that * compound allowed regardless if this pattern otherwise matches". No * dictionary uses this feature and it isn't implemented in either Spylls * or Espells. */ export class CompoundPattern { /** * @param left - The `endchars[/flag]` syntax. * @param right - The `beginchars[/flag]` syntax. * @param _replacement - An unused optional syntax. See the documentation * for the class itself for more info. Unused in both Spylls and Espells. */ constructor(left, right, _replacement) { // @ts-ignore ; (this.left = { noAffix: false }), (this.right = { noAffix: false }); [this.left.stem, this.left.flag = ""] = left.split("/"); [this.right.stem, this.right.flag = ""] = right.split("/"); if (this.left.stem === "0") { this.left.stem = ""; this.left.noAffix = true; } if (this.right.stem === "0") { this.right.stem = ""; this.right.noAffix = true; } } /** * Determines if a pair of {@link AffixForm}s isn't an allowed compound * pair, as in this returns `true` if the pair is invalid. * * @param left - The left-side {@link AffixForm}. * @param right - The right-side {@link AffixForm}. */ match(left, right) { // prettier-ignore return ( // stems match (!this.left.stem || left.stem.endsWith(this.left.stem)) && (!this.right.stem || right.stem.startsWith(this.right.stem)) && // check for zero affixes case (!this.left.noAffix || !left.hasAffixes) && (!this.right.noAffix || !right.hasAffixes) && // check flags (!this.left.flag || left.flags.has(this.left.flag)) && (!this.right.flag || right.flags.has(this.right.flag))); } } //# sourceMappingURL=compound-pattern.js.map