@dcoffey/espells
Version:
Pure JS/TS spellchecker, using Hunspell dictionaries. Based on Spylls.
50 lines • 1.69 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 { decoder } from "./constants.js";
/** Wrapper for manipulating a document separated by lines. */
export class Reader {
/** @param input - Document to read. */
constructor(input) {
// clean input
if (typeof input !== "string")
input = decoder.decode(input);
input = input.replaceAll("\r\n", "\n");
this.lines = input.split("\n");
this.index = 0;
this.line = this.lines[0];
}
/** True if the reader has reached the end of the document. */
get done() {
return this.index >= this.lines.length;
}
/** Advances the reader one line. */
next() {
if (this.done)
return null;
this.index++;
this.line = this.lines[this.index];
return true;
}
/**
* Advances the reader a given number of steps, calling a callback
* function for each line it steps through.
*
* @param steps - The number of lines to step through.
* @param cb - The callback function to call. Return false to further advancing.
*/
for(steps, cb) {
if (!steps)
return;
const startIndex = this.index + 1;
for (let idx = 0; idx < steps; idx++) {
if (this.done)
break;
this.index = startIndex + idx;
this.line = this.lines[this.index];
if (cb(this.line) === false)
break;
}
}
}
//# sourceMappingURL=reader.js.map