A lightweight vanilla javascript app to count the occurance of a word or a phrase in a string.
npm i @tyah/count-words
- Cloning repository
git clone https://github.com/gumbol/count-words
- CommonJS:
const countWords = require("path/to/count-words")
- ES modules for the browser: You'd need to comment module.exports in count-words.js and uncomment exports default.
//module.exports = (string, word) =>
export default (string, word) =>
<script type="module">
import countWords from "./path/to/count-words/count-words.js"
</script>
console.log(countWords("fooBarFOobazBar fOo f oo F oO", "fobar"))
// -> No match
console.log(countWords("fooBarFOobazBar fOo f oo F oO", "f oo"))
// -> 2
console.log(countWords("fooBarFOobazBar fOo f oo F oO", "fooBaz"))
// -> 1
Inspired by exercises from Eloquent Javascript book by Marijn Haverbeke