word-counter-ac
Version:
A simple word counter CLI package
21 lines (15 loc) • 510 B
JavaScript
import fs from 'node:fs/promises'
const [,,filePath, word] = process.argv;
const textContent = await fs.readFile(filePath, 'utf-8')
const wordsCount = {};
const words = textContent.split(/[\W]/).filter(w => w);
for (let word of words) {
word = word.toLowerCase();
if(!(word in wordsCount)) {
wordsCount[word] = 0;
}
wordsCount[word] += 1;
}
// console.log(wordsCount)
console.log(`The word '${word}' has occured ${wordsCount[word]} times.`)