UNPKG

@dcoffey/espells

Version:

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

40 lines 1.6 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/. */ import { re, replaceRange } from "../util.js"; /** * A replacement pattern describes a pattern and its replacement, with the * pattern matching a common typo in a word, and the replacement being the * relevant correction for that typo. It uses the following syntax: * * ```text * REP <number of entries> * REP <pattern> <replacement> * ``` * * The `pattern` syntax supports `^` and `$` anchors, like `RegExp`. In the * `replacement` string, a `_` underscore can be used in the string to * represent a space, e.g. correcting `alot` to `a lot`, using the * `replacement` `a_lot`. */ export class RepPattern { /** * @param pattern - The pattern to replace. Is treated as a `RegExp`, but * given as a string. * @param replacement - The string to replace anything matched by `pattern` with. */ constructor(pattern, replacement) { this.pattern = re `/${pattern}/g`; this.replacement = replacement.replaceAll("_", " "); } /** Yields the permutations of a word with this `RepPattern` applied to it. */ *replace(word) { for (const match of word.matchAll(this.pattern)) { const from = match.index; const to = from + this.replacement.length; const replaced = replaceRange(word, from, to, this.replacement); yield replaced; } } } //# sourceMappingURL=rep-pattern.js.map