Skip to the content.

Count Words

Code Coverage Deno Doc

A lightweight JavaScript/TypeScript library to count words in a string. It handles multiple consecutive spaces correctly and is Unicode-aware.

Features

Quick Start

Deno πŸ¦•

import { countWords } from "https://deno.land/x/count_words/mod.ts";

console.log(countWords("Hello World")); // 2
console.log(countWords("The quick brown fox")); // 4

Node.js πŸ’πŸš€

import { countWords } from "count-n-words";

console.log(countWords("Hello World")); // 2
console.log(countWords("The quick brown fox")); // 4

Browser 🌐

You can use any CDN πŸ”₯

<script type="module">
  import { countWords } from "https://cdn.skypack.dev/count-n-words";

  console.log(countWords("Hello World")); // 2
</script>

Other CDN options:

API Reference

countWords(text: string, separator?: string): number

Counts the number of words in a string.

Parameters

Returns

Examples

// Basic usage
countWords("Hello World"); // 2
countWords("Hey, my name is Eliaz."); // 5

// Handles multiple spaces
countWords("Hello        World"); // 2
countWords("  Hey  "); // 1

// Empty strings
countWords(""); // 0
countWords("   "); // 0

// Custom separator
countWords("apple,banana,orange", ","); // 3

Documentation

Full API documentation is hosted on Deno Doc πŸ“„

Support

Open an Issue, I will check it a soon as possible πŸ‘€

If you want to hurry me up a bit send me a tweet πŸ˜†

Consider supporting me on Patreon if you like my work πŸ™

Don’t forget to start the repo ⭐

Authors

Eliaz Bobadilla - Creator and Maintainer πŸ’ͺ

See also the full list of contributors who participated in this project ✨

Versioning

We use Semantic Versioning. For the versions available, see the tags 🏷️

Licence

Licensed under the MIT License πŸ“„