word-counter-cli-roshan
Version:
A simple CLI to count words in a file
25 lines (21 loc) • 575 B
JavaScript
import {readFile} from "fs/promises"
const fileName=process.argv[2] || "file-1.txt";
const specialSearch=process.argv[3]
const fileContent=await readFile(`./${fileName}`, "utf-8")
const fileDataArray=fileContent.split(/[\W]/).filter((w)=> w)
let wordObject={}
fileDataArray.forEach((word)=>{
if(word in wordObject){
wordObject[word] += 1;
}
else{
wordObject[word]=1;
}
})
if(specialSearch){
console.log(`${specialSearch} : ${wordObject[specialSearch]}`)
}
else{
console.log(wordObject)
}