npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

text-analyzer

v3.0.1

Published

Analyze text: count characters, words, sentences, paragraphs, and reading time

Readme

text-analyzer

Analyze text: count characters, words, sentences, paragraphs, and reading time.

Installation

# npm
npm install text-analyzer

# pnpm
pnpm add text-analyzer

# bun
bun add text-analyzer

Usage

import {
	countCharacters,
	countLines,
	countParagraphs,
	countSentences,
	countSequenceOccurrences,
	countWords,
	getAverageWordLength,
	getReadingTime,
	getWordFrequency,
} from "text-analyzer"

API

countCharacters(text, options?)

Count the number of characters in a text.

  • options.unit: "grapheme" (default) counts user-perceived characters (e.g. the emoji "👨‍👩‍👧" counts as 1). "code-unit" counts UTF-16 code units, matching String.prototype.length.
  • options.locale: BCP 47 locale tag passed to Intl.Segmenter. Only used when unit is "grapheme".
  • options.normalize: when true, normalize the text to NFC before counting. Defaults to false.
countCharacters("text") // 4
countCharacters("👨‍👩‍👧") // 1
countCharacters("👨‍👩‍👧", { unit: "code-unit" }) // 8

countWords(text)

Count the number of words in a text. Words are separated by any whitespace. Note: punctuation stays attached, so "hello, world" counts as 2 words ("hello," and "world"). For a linguistic word count, use getWordFrequency.

countWords("one two three") // 3
countWords("  one\ttwo\r\nthree  ") // 3

countLines(text)

Count the number of lines in a text. Handles \n, \r\n, and \r. A trailing line terminator does not add an extra empty line.

countLines("one\ntwo\nthree") // 3
countLines("one\n") // 1

countSentences(text, options?)

Count the number of sentences using Intl.Segmenter, so decimals and abbreviations don't accidentally split a sentence.

  • options.locale: BCP 47 locale tag passed to Intl.Segmenter.
countSentences("Hello. World!") // 2
countSentences("The value is 3.14. Done.") // 2

countParagraphs(text)

Count the number of paragraphs. Paragraphs are separated by one or more blank lines.

countParagraphs("one\n\ntwo\n\n\nthree") // 3

countSequenceOccurrences(text, sequence, options?)

Count the number of times a sequence occurs in a text.

  • options.caseSensitive: defaults to true.
  • options.overlapping: when true, overlapping matches are counted (e.g. "aa" matches 3 times in "aaaa"). Defaults to false.
  • options.locale: BCP 47 locale tag used for case folding (only relevant when caseSensitive is false).
  • options.normalize: when true, normalize both text and sequence to NFC before searching. Defaults to false.
countSequenceOccurrences("dolor Dolor dolor", "dolor") // 2
countSequenceOccurrences("dolor Dolor dolor", "dolor", { caseSensitive: false }) // 3
countSequenceOccurrences("aaaa", "aa") // 2
countSequenceOccurrences("aaaa", "aa", { overlapping: true }) // 3

getWordFrequency(text, options?)

Count how many times each word occurs in a text. Words are detected with Intl.Segmenter, so punctuation is excluded and contractions are kept as one word. Returns a Map<string, number> sorted by count in descending order.

  • options.caseSensitive: defaults to true. Pass false for typical natural-language frequency analysis where "The" and "the" should be treated as the same word.
  • options.locale: BCP 47 locale tag passed to Intl.Segmenter and used for case folding.
getWordFrequency("The cat sat on the mat.")
// Map { "The" => 1, "cat" => 1, "sat" => 1, "on" => 1, "the" => 1, "mat" => 1 }

getWordFrequency("The cat sat on the mat.", { caseSensitive: false })
// Map { "the" => 2, "cat" => 1, "sat" => 1, "on" => 1, "mat" => 1 }

getAverageWordLength(text, options?)

Compute the average length of words in a text. Returns 0 when the text contains no words. Word splitting is whitespace-based, matching countWords.

  • options.unit: passed to countCharacters ("grapheme" by default).
  • options.locale: passed to countCharacters.
getAverageWordLength("aa bbb cccc") // 3

getReadingTime(text, options?)

Estimate the reading time for a text.

  • options.wordsPerMinute: reading speed. Must be greater than 0. Defaults to 200.
getReadingTime("one two three")
// { words: 3, minutes: 0.015, milliseconds: 900 }

getReadingTime("one two three", { wordsPerMinute: 100 })
// { words: 3, minutes: 0.03, milliseconds: 1800 }

License

MIT