-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (31 loc) · 971 Bytes
/
index.js
File metadata and controls
40 lines (31 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import minimist from 'minimist';
(async () => {
const fetch = (await import('node-fetch')).default;
async function fetchAndCountBodyWords(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok.');
}
countBodyOccurrences(await response.text());
} catch (error) {
console.error('There was a problem with the fetch operation.');
}
}
function countBodyOccurrences(content) {
const matches = content.match(new RegExp(`\\bbody\\b`, 'gi'));
const count = matches ? matches.length : 0;
console.log(
count === 1
? `Your body count is: ${count} body.`
: `Your body count is: ${count} bodies.`
);
}
const args = minimist(process.argv.slice(2));
const url = args.url;
if (!url) {
console.error('Please provide a URL with --url.');
process.exit(1);
}
await fetchAndCountBodyWords(url);
})();