Install reading-time via npm
masterInstall the reading-time package using npm for your production environment.
npm install reading-time --productionrepository·master·Indexed 23 days ago
https://github.com/ngryman/reading-timeA utility for estimating reading time from plain text, markdown, and HTML. Version 2.0.0-1 provides functions like readingTime(), countWords(), and readingTimeWithCount(), as well as a ReadingTimeStream class for processing text via Node.js streams. It supports customizable reading speeds (wordsPerMinute) and custom word boundary predicates.
Install the reading-time package using npm for your production environment.
npm install reading-time --productionYou can use the readingTime function to estimate reading time from text.
Node.js usage: Import from the main entrypoint.
Browser usage: Import from reading-time/lib/reading-time to avoid issues with the ReadingTimeStream class which is not supported in browsers without polyfills.
// In Node.js
const readingTime = require('reading-time');
// In the browser
const readingTime = require('reading-time/lib/reading-time');
const stats = readingTime(text);
// stats: { minutes: 1, time: 60000, words: { total: 200 } }
console.log(`The reading time is: ${stats.minutes} min`);The ReadingTimeStream class allows you to process text via Node.js streams. You can pipe a readable stream into the analyzer and listen for the data event, which emits the word count.
const {ReadingTimeStream, readingTimeWithCount} = require('reading-time');
const fs = require('fs');
const analyzer = new ReadingTimeStream();
fs.createReadStream('foo')
.pipe(analyzer)
.on('data', (count) => {
console.log(`The reading time is: ${readingTimeWithCount(count).minutes} min`);
});Returns an object representing the word count statistics for the provided text.
Parameters:
text: The string to analyze.options (optional):wordBound: A function returning a boolean to determine if a character is a word boundary (default: spaces, new lines, and tabs).type WordCountStats = {
total: number;
};Calculates reading time statistics using pre-calculated word count stats. This is useful when working with streams or when you already have the word count.
Parameters:
words: A WordCountStats object.options (optional):wordsPerMinute: The average reading speed (default: 200).Note: readingTime(text, options) === readingTimeWithCount(countWords(text, options), options).
Returns an object containing estimated reading time statistics based on the provided text.
Parameters:
text: The string to analyze.options (optional):wordsPerMinute: The average reading speed (default: 200).wordBound: A function returning a boolean to determine if a character is a word boundary (default: spaces, new lines, and tabs).type ReadingTimeResults = {
minutes: number;
time: number;
words: WordCountStats;
};Both readingTime, countWords, and readingTimeWithCount accept an optional Options object to customize the calculation:
wordsPerMinute: (number) The assumed reading speed. Defaults to 200.wordBound: (function) A custom predicate function used to identify word boundaries (e.g., whitespace or newlines). By default, it uses isAnsiWordBound which checks for ' \n\r\t'.readingTime is used to estimate the reading time of a given text. It accepts a string of text and an optional configuration object.readingTime function is the primary entry point for calculating both the estimated reading time and the total word count of a given string. It returns an object containing the estimated minutes, the precise time in milliseconds, and the word count statistics.readingTimeWithCount to estimate the reading time directly from that number. It accepts a number of words and an optional configuration object.ReadingTimeStream class provides a way to calculate reading time using Node.js streams, which is useful for processing large amounts of text data without loading it all into memory at once.The ReadingTimeStream class is a Node.js Transform stream (in objectMode) that processes text chunks to calculate cumulative word count statistics.
As chunks of text pass through the stream, it uses the underlying countWords logic to update an internal stats.total counter. When the stream is finished (during the _flush phase), it pushes a single WordCountStats object containing the total word count to the next stage of the pipeline.
To use it, pipe a text source (like a readable file stream) into ReadingTimeStream. The final output of the stream will be an object of type WordCountStats.