A lightweight JavaScript/TypeScript library to count words in a string. It handles multiple consecutive spaces correctly and is Unicode-aware.
- β¨ Simple and intuitive API
- π Fast and efficient word counting
- π§ Handles multiple consecutive spaces
- π Unicode-aware
- π¦ Works with Deno, Node.js, and browsers
- π― Zero dependencies
import { countWords } from "https://deno.land/x/count_words/mod.ts";
console.log(countWords("Hello World")); // 2
console.log(countWords("The quick brown fox")); // 4import { countWords } from "count-n-words";
console.log(countWords("Hello World")); // 2
console.log(countWords("The quick brown fox")); // 4You 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:
Counts the number of words in a string.
text(string): The text to count words inseparator(string, optional): The separator between words. Defaults to" "(space)
- (number): The number of words found in the text
// 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", ","); // 3Full API documentation is hosted on Deno Doc π
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 β
Eliaz Bobadilla - Creator and Maintainer πͺ
See also the full list of contributors who participated in this project β¨
We use Semantic Versioning. For the versions available, see the tags π·οΈ
Licensed under the MIT License π