reading-time

repository·master·Indexed 23 days ago

https://github.com/ngryman/reading-time

A 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.

Tokens
1.6K
Snippets
6
Records
16
Agent score
79%

What's inside reading-time

  1. Use reading-time in Node.js or the browser

    master

    You 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`);
  2. Use ReadingTimeStream for streaming data

    master

    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`);
      });
  3. countWords(text, options?)

    master

    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;
    };
  4. readingTimeWithCount(words, options?)

    master

    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).

  5. readingTime(text, options?)

    master

    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;
    };
  6. Configure readingTime options

    master

    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'.
  7. Estimate reading time with readingTime()

    master
    The 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.
  8. Use ReadingTimeStream for stream-based word counting

    master

    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.