Count Words
A lightweight JavaScript/TypeScript library to count words in a string. It handles multiple consecutive spaces correctly and is Unicode-aware.
Features
- β¨ Simple and intuitive API
- π Fast and efficient word counting
- π§ Handles multiple consecutive spaces
- π Unicode-aware
- π¦ Works with Deno, Node.js, and browsers
- π― Zero dependencies
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
text(string): The text to count words inseparator(string, optional): The separator between words. Defaults to" "(space)
Returns
- (number): The number of words found in the text
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 π