wcounterx 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/Readme.md +15 -0
  2. package/bin/index.js +32 -0
  3. package/package.json +12 -0
package/Readme.md ADDED
@@ -0,0 +1,15 @@
1
+ # Word Counter CLI
2
+
3
+ A simple CLI tool to count occurrences of a word in a file.
4
+
5
+ ## Installation
6
+
7
+ npx word-counter-cli <file-path> <word>
8
+
9
+ OR
10
+
11
+ npm install -g word-counter-cli
12
+
13
+ ## Usage
14
+
15
+ word-counter file.txt hello
package/bin/index.js ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env node
2
+ import fs from 'node:fs/promises';
3
+ try {
4
+ const file1Content = await fs.readFile(process.argv[2], 'utf-8');
5
+
6
+ const wordsArr = file1Content.split(/[\W]/).filter((w) => w);
7
+
8
+ const wordsCount = {};
9
+
10
+ wordsArr.forEach((word) => {
11
+ word = word.toLowerCase()
12
+ if (word in wordsCount) {
13
+ wordsCount[word] += 1;
14
+ } else {
15
+ wordsCount[word] = 1;
16
+ }
17
+ });
18
+ let search = process.argv[3];
19
+ search = search?.toLowerCase()
20
+ if (search in wordsCount) {
21
+ console.log(wordsCount[search]);
22
+ }
23
+ else if(search === undefined){
24
+ console.log(wordsCount)
25
+ }
26
+ else{
27
+ console.log("Word not found...")
28
+ }
29
+ } catch (error) {
30
+ console.log('error: file not found');
31
+ }
32
+
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "wcounterx",
3
+ "version": "1.0.0",
4
+ "description": "A CLI tool to count occurrences of a word in a file",
5
+ "main": "bin/index.js",
6
+ "bin": {
7
+ "word-counter": "./bin/index.js"
8
+ },
9
+ "type": "module",
10
+ "keywords": ["cli", "word", "counter", "file", "nodejs"],
11
+ "author": "Khushboo"
12
+ }