Skip to content

Commit 2552f7f

Browse files
committed
prep
1 parent b5089a2 commit 2552f7f

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "prep",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"commander": "^14.0.3"
14+
}
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { program } from 'commander';
2+
import process from 'node:process';
3+
import { promises as fs } from 'node:fs';
4+
5+
program
6+
.name('prep')
7+
.description('Counts the number of words in a file that contain the letter "e".')
8+
.option('-c, --char <char>', 'The character to search for in words', 'e')
9+
.argument('<path>', 'The path to the file to analyze')
10+
11+
program.parse();
12+
13+
const argv = program.args;
14+
15+
if (argv.length != 1) {
16+
console.error(
17+
`Expected exactly 1 argument (a path) to be passed but got ${argv.length}.`,
18+
);
19+
process.exit(1);
20+
}
21+
const path = argv[0];
22+
const char = program.opts().char;
23+
24+
const content = await fs.readFile(path, 'utf-8');
25+
const countOfWordsContainingChar = content
26+
.split(' ')
27+
.filter((word) => word.includes(char)).length;
28+
console.log(countOfWordsContainingChar);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is a test. there should be 4 in the output

0 commit comments

Comments
 (0)