UNPKG

lesetid

Version:

A dead simple read time estimation

44 lines (42 loc) 1.26 kB
import "./utils-CnJJfLNX.js"; import { DEFAULT_OPTIONS, count } from "./src-BHHz38yF.js"; import { Transform } from "node:stream"; //#region src/stream.ts /** * Creates a new reading time stream transform that calculates reading statistics. * * @param {Options} options - Configuration options for reading time calculation * @returns {ReadingTimeStream} A new ReadingTimeStream instance */ function createReadingTimeStream(options = DEFAULT_OPTIONS) { return new ReadingTimeStream(options); } /** * A Transform stream that calculates reading time statistics from text data. * * This stream processes text chunks and accumulates word and character counts, * then outputs the final reading statistics when the stream ends. */ var ReadingTimeStream = class extends Transform { reading = { words: 0, chars: 0 }; options; constructor(options = DEFAULT_OPTIONS) { super({ objectMode: true }); this.options = options; } _transform(chunk, encoding, callback) { const { words, chars } = count(chunk.toString(encoding), this.options); this.reading.words += words; this.reading.chars += chars; callback(); } _flush(callback) { this.push(this.reading); callback(); } }; //#endregion export { ReadingTimeStream, createReadingTimeStream };