Skip to content

Commit ddcf9ae

Browse files
committed
Implement wc tool with line word and character counts
1 parent 006540b commit ddcf9ae

7 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require("fs");
2+
const file = "./sample-files/3.txt";
3+
const content = fs.readFileSync(file, "utf-8");
4+
5+
let lines = 0;
6+
7+
const line = content.split("\n").length - 1;
8+
lines += line;
9+
console.log(lines, file);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require("fs");
2+
const file = "./sample-files/3.txt";
3+
const content = fs.readFileSync(file, "utf-8");
4+
5+
let characters = 0;
6+
7+
const char = content.length;
8+
characters += char;
9+
console.log(characters, file);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const fs = require("fs");
2+
const folder = "./sample-files";
3+
const content = fs.readdirSync(folder);
4+
5+
let totalLines = 0;
6+
let totalWords = 0;
7+
content.forEach((file) => {
8+
const path = `${folder}/${file}`;
9+
const filePath = fs.readFileSync(path, "utf-8");
10+
11+
const line = filePath.split("\n").length - 1;
12+
const word = filePath.split(/\s+/).length - 1;
13+
14+
totalLines += line;
15+
totalWords += word;
16+
console.log(line, word, path);
17+
});
18+
19+
console.log(totalLines, totalWords, "total");
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require("fs");
2+
const file = "./sample-files/3.txt";
3+
const content = fs.readFileSync(file, "utf-8");
4+
5+
const line = content.split("\n").length - 1;
6+
const word = content.split(/\s+/).length - 1;
7+
8+
console.log(line, word, file);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const fs = require("fs");
2+
const folder = "./sample-files";
3+
const filesPath = fs.readdirSync(folder);
4+
5+
let totalLines = 0;
6+
7+
filesPath.forEach((file) => {
8+
const path = `${folder}/${file}`;
9+
const files = fs.readFileSync(path, "utf-8");
10+
11+
const lines = files.split("\n").length - 1;
12+
console.log(lines, path);
13+
14+
totalLines += lines;
15+
});
16+
console.log(totalLines, "total");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const fs = require("fs");
2+
const file = "./sample-files/3.txt";
3+
const content = fs.readFileSync(file, "utf-8");
4+
5+
const word = content.split(/\s+/).length - 1;
6+
7+
console.log(word, file);

implement-shell-tools/wc/wc.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Power shell commands =======> Vs <===== Js codes
2+
3+
// wc sample-files/*
4+
5+
const fs = require("fs");
6+
const filePath = "./sample-files";
7+
const contents = fs.readdirSync(filePath);
8+
9+
let totalLines = 0;
10+
let totalWords = 0;
11+
let totalCharacters = 0;
12+
13+
contents.forEach((file) => {
14+
const path = `${filePath}/${file}`;
15+
16+
const files = fs.readFileSync(path, "utf-8");
17+
18+
const lines = files.split("\n").length - 1;
19+
const words = files.trim().split(/\s+/).length;
20+
const chars = files.length;
21+
22+
console.log(lines, words, chars, path);
23+
24+
totalLines += lines;
25+
totalWords += words;
26+
totalCharacters += chars;
27+
});
28+
console.log(totalLines, totalWords, totalCharacters, "total");

0 commit comments

Comments
 (0)