estimated-reading-time
Calculate the estimated reading time of a given text (plain text or HTML)
Installation
Browser:
Node:
npm i estimated-reading-time
API
estimatedReadingTime( text: string, format: TextFormat, options: Options ): Result
Parameters:
parameter | type | description |
---|---|---|
text | string |
The text you want to estimate |
format | string or TextFormat |
Can be either html or plain_text . You can also use the exported member TextFormat as: TextFormat.HTML or TextFormat.PLAIN_TEXT |
options | any or Options |
{ isTechnical: boolean, wordsPerMinute: number } . The isTechnical param sets the default 250 words per minute to 150 . You can also pass in a custom words per minute metric using the wordsPerMinute option. |
Result:
parameter | type | description |
---|---|---|
numWords | number |
The number of readable words of the text |
minutes | number |
The estimated minutes it would cost reading the full text |
seconds | number |
The remaining seconds (e.g it would cost 7 minutes and 31 seconds ) (this is not the total seconds it would cost, but the remaining from the minutes) |
roundedMinutes | number |
If it costs more than x minutes and 30 seconds it rounds up to the next minute (e.g 7 minutes and 40 seconds = 8 minutes ) |
Usage:
Browser
Using the package in the browser, all the functions are encapsulated inside the EstimatedReadingTime
global object.
Plain text
<script> // Suppose longer text const plainText = `En un lugar de la Mancha, de cuyo nombre no quiero acordarme, no ha mucho tiempo que vivía un hidalgo de los de lanza en astillero, adarga antigua, rocín flaco y galgo corredor. Una olla de algo más vaca que carnero, salpicón las más noches, duelos y quebrantos los sábados, lantejas los viernes, algún palomino de añadidura los domingos, consumían las tres partes de su hacienda....`; const res = EstimatedReadingTime; console;</script>
res
:
HTML
<script> const htmlText = ` <div class="content"> <h1>Lorem ipsum</h1> </div> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempora placeat eaque dolore reprehenderit error animi veritatis? Corporis animi, sapiente ex voluptate, repudiandae nihil fugit soluta fugiat perferendis consectetur, quae id.</p> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Eius accusamus, voluptatibus hic earum quas ea asperiores? Eos nostrum tempora eius impedit, perspiciatis explicabo maxime labore, deserunt ad vel, excepturi repudiandae!</p> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam pariatur, esse nisi nesciunt nam consequatur voluptatem magnam necessitatibus perferendis eius recusandae fugiat, adipisci numquam optio! Distinctio magni dicta ex corrupti.</p> `; // Here we use EstimatedReadingTime.TextFormat.HTML instead of the string "html" const res = EstimatedReadingTime; console;</script>
res
:
If you'd want to get the html directly from any element on the page:
// ...const htmlText = documentinnerHTML;// ...
Node (CommonJS)
const ert = ; const estimatedReadingTime TextFormat = ert;const text = `... any text you want to estimate ...`;const res = ;console;
Node (ESM)
Note that in order to use ESM imports your file should have a
.mjs
extension or either the closerpackage.json
should be oftype: "module"
. Also note that depending on your Node.js version, you may be required to run the script using the--experimental-modules
flag.
; const text = `... any text you want to estimate ...`;const res = ;console;
TypeScript
You can also use the package in your typescript
code!
; ;;;console.logres;
package.json
: