Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions implement-shell-tools/cat/cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { promises as fs } from "node:fs";

// get all command line arguments after "node cat.js" - like -n or -b
const args = process.argv.slice(2);

// put them in variables
const showAllNumbers = args.includes("-n");
const showNonEmptyNumbers = args.includes("-b");

// getting the paths of files
const paths = args.filter(arg => arg !== "-n" && arg !== "-b");

// loop over each file
for (const path of paths) {
try {
// read file as text
const content = await fs.readFile(path, "utf-8");

// split them into lines
const lines = content.split("\n");

let lineNumber = 1; // tracks line numbers for -b and -n

lines.forEach((line) => {
if (showNonEmptyNumbers) {
// -b: number only non-empty lines
if (line.trim() !== "") {
console.log(`${lineNumber} ${line}`);
lineNumber++;
} else {
console.log(line); // shows empty line with no number
}
} else if (showAllNumbers) {
// -n: number all lines
console.log(`${lineNumber} ${line}`);
lineNumber++;
} else {
// no flags: just print line
console.log(line);
}
});
} catch (err) {
console.error(`Error reading file "${path}": ${err.message}`);
}
}
28 changes: 28 additions & 0 deletions implement-shell-tools/ls/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { promises as fs } from "node:fs";

//getting all commands
const args = process.argv.slice(2);

const showOnePerLine = args.includes("-1");
const showAllFilesWithHidden = args.includes("-a");

//current path
const path = args.find(arg => !arg.startsWith("-")) || ".";
// if we do console.log("path=> ",path," args=> " ,args); it will give us this =>: path=> sample-files args=> [ '-1', '-a', 'sample-files' ]

const direc = await fs.readdir(path)
// if the path is <sample-files> console.log(direc) gives us =>: [ '.hidden.txt', '1.txt', '2.txt', '3.txt', 'dir' ]

if(showOnePerLine && showAllFilesWithHidden){
direc.forEach(element => {
console.log(element)
})
}
else if(showOnePerLine){
const visibleFiles = direc.filter(element => !element.startsWith(".")); // filtering hidden files which starts with "."
visibleFiles.forEach(element => {
console.log(element)
});
}


2 changes: 1 addition & 1 deletion implement-shell-tools/wc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ It must act the same as `wc` would, if run from the directory containing this RE

Matching any additional behaviours or flags are optional stretch goals.

We recommend you start off supporting no flags for one file, then add support for multiple files, then add support for the flags.
We recommend you start off supporting no flags for one file, then add support for multiple files, then add support for the flags
60 changes: 60 additions & 0 deletions implement-shell-tools/wc/wc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { promises as fs } from "node:fs";

const args = process.argv.slice(2);
console.log(args)

const showLines = args.includes("-l");
const showWords = args.includes("-w");
const showChars = args.includes("-c");
const paths = args.filter(arg => !arg.startsWith("-")) || ".";
console.log(paths)

// const direct = await fs.readdir(path);

// console.log(direct);
let totalLines = 0;
let totalWords = 0;
let totalchars = 0;

if(showLines){
for (const path of paths){
const content = await fs.readFile(path, "utf-8");
const lines = content.split("\n").length;

totalLines += lines;
}
console.log("line: ", totalLines);
}
else if(showWords){
for (const path of paths){
const content = await fs.readFile(path, "utf-8");
const words = content.split(/\s+/).filter(Boolean).length;

totalWords += words;
}
console.log("words: ", totalWords);
}
else if(showChars){
for (const path of paths){
const content = await fs.readFile(path, "utf-8");
const char = content.length;

totalchars += char;
}
console.log("chars", totalchars)
}
else{
for (const path of paths){
const content = await fs.readFile(path, "utf-8");
const lines = content.split("\n").length;
const words = content.split(/\s+/).filter(Boolean).length;
const char = content.length;

totalLines += lines;
totalWords += words;
totalchars += char;
}
console.log("lines: ", totalLines, " words", totalWords, " char:", totalchars)
}


Loading