From bf584328d4ca4f349a752dff929e63c3416c2a43 Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Sat, 7 Feb 2026 18:44:19 +0000 Subject: [PATCH 1/4] impl shell tools done --- implement-shell-tools/cat/index.js | 87 ++++++++++++++++++++++++++++++ implement-shell-tools/cat/notes.js | 46 ++++++++++++++++ implement-shell-tools/ls/index.js | 49 +++++++++++++++++ implement-shell-tools/wc/index.js | 77 ++++++++++++++++++++++++++ 4 files changed, 259 insertions(+) create mode 100644 implement-shell-tools/cat/index.js create mode 100644 implement-shell-tools/cat/notes.js create mode 100644 implement-shell-tools/ls/index.js create mode 100644 implement-shell-tools/wc/index.js diff --git a/implement-shell-tools/cat/index.js b/implement-shell-tools/cat/index.js new file mode 100644 index 000000000..cb799d819 --- /dev/null +++ b/implement-shell-tools/cat/index.js @@ -0,0 +1,87 @@ +import process from "node:process"; +import { promises as fs } from "node:fs"; + + +//one for 1 and 3 +async function printOneOrMore(listOfFiles) { + for (const file of listOfFiles) { + const content = await fs.readFile(file, "utf-8"); + console.log(content); + } +} + + +// // 2 * `cat -n sample-files/1.txt` +// async function caseTwo(oneFile) { +// const content = await fs.readFile(oneFile, "utf-8"); +// const separatedToLines = content.split('\n') +// separatedToLines.forEach((line, index) => { +// console.log(`${index + 1} ${line}`) +// }) +// } + +// // 4 * `cat -n sample-files/*.txt` +// async function caseFour(listOfFiles) { +// for (const file of listOfFiles) { +// const content = await fs.readFile(one, "utf-8"); + +// const separatedToLines = content.split('\n') +// separatedToLines.forEach((line, index) => { +// console.log(`${index + 1} ${line}`) +// }) +// } + +// } + +//one instead +// 2 * `cat -n sample-files/1.txt` +// 4 * `cat -n sample-files/*.txt` + +async function caseTwoAndFour(listOfFiles) { + for (const file of listOfFiles) { + const content = await fs.readFile(file, "utf-8"); + + const separatedToLines = content.split('\n') + separatedToLines.forEach((line, index) => { + console.log(`${index + 1} ${line}`) + }) +} + +} + +// `cat -b sample-files/3.txt` +async function caseFive(listOfFiles) { + for (const file of listOfFiles) { + const content = await fs.readFile(file, "utf-8"); + const separatedToLines = content.split('\n'); + + let countingOnlyFullLines = 1 + separatedToLines.forEach((line, index) => { + if (line !== '') { + console.log(`${countingOnlyFullLines} ${line}`) + countingOnlyFullLines++ + } else { + console.log('') + } + + }) + } + +} + + +const argv = process.argv.slice(2); + +switch (argv[0]) { + case '-n': + await caseTwoAndFour(argv.slice(1)); + break; + + case '-b': + await caseFive(argv.slice(1)); + break; + + default: + await printOneOrMore(argv); + break; +} \ No newline at end of file diff --git a/implement-shell-tools/cat/notes.js b/implement-shell-tools/cat/notes.js new file mode 100644 index 000000000..87f4fd2c1 --- /dev/null +++ b/implement-shell-tools/cat/notes.js @@ -0,0 +1,46 @@ +import process from "node:process"; +import { promises as fs } from "node:fs"; +// const fs = promises + +// * `cat sample-files/1.txt` +// * `cat -n sample-files/1.txt` +// * `cat sample-files/*.txt` +// * `cat -n sample-files/*.txt` +// * `cat -b sample-files/3.txt` + +// process.argv documentation that process.argv[0] will be the path to node +// process.argv[1] will be the path to this file +// the arguments start at index 2 + +const argv = process.argv.slice(2); +if (argv.length != 1) { + console.error(`Expected exactly 1 argument (a path) to be passed but got ${argv.length}.`); + process.exit(1); +} +const path = argv[0]; + +// const content = await fs.readFile(path, "utf-8"); +// const countOfWordsContainingEs = content +// .split(" ") +// .filter((word) => word.includes("e")) +// .length; +// console.log(countOfWordsContainingEs); + +// `cat sample-files/1.txt` +const content = await fs.readFile('sample-files/1.txt', "utf-8"); +const contentsOfFileOne = content +console.log(contentsOfFileOne); + +// * `cat -n sample-files/1.txt` +const separatedToLines = content.split('\n') +separatedToLines.forEach((line, countLine) => { + countLine+=1 + console.log(line, countLine}) +const withAddedCount = countFileOne. + +// * `cat sample-files/*.txt` + +// * `cat -n sample-files/*.txt` + +// * `cat -b sample-files/3.txt` + diff --git a/implement-shell-tools/ls/index.js b/implement-shell-tools/ls/index.js new file mode 100644 index 000000000..5dc90f6ac --- /dev/null +++ b/implement-shell-tools/ls/index.js @@ -0,0 +1,49 @@ +// fs.readdir +import process from "node:process"; +import { promises as fs } from "node:fs"; + + +// `ls -1` +async function showAllFilesInDir(directory) { + const listOfFiles = await fs.readdir(directory); + + for (const eachFile of listOfFiles) { + console.log(eachFile); + } +} + +// `ls -1 sample-files` +async function showVisibleInSampleFiles() { + const listOfFiles = await fs.readdir('sample-files'); + + for (const eachFile of listOfFiles) { + if (eachFile[0] !== '.') { + console.log(eachFile); + } + + } +} + + +// `ls -1 -a sample-files` +async function showAllInSampleFiles() { + const listOfFiles = await fs.readdir('sample-files'); + + for (const eachFile of listOfFiles) { + + console.log(eachFile); + + } +} + + + +const argv = process.argv.slice(2); + +if (argv.includes('-a')) { + await showAllInSampleFiles(); +} else if (argv.includes('sample-files')) { + await showVisibleInSampleFiles(); +} else { + await showCurrentDir(); +} diff --git a/implement-shell-tools/wc/index.js b/implement-shell-tools/wc/index.js new file mode 100644 index 000000000..92a366b95 --- /dev/null +++ b/implement-shell-tools/wc/index.js @@ -0,0 +1,77 @@ +import process from "node:process"; +import { promises as fs } from "node:fs"; + +//from coursework +// const content = await fs.readFile(path, "utf-8"); +// const countOfWordsContainingEs = content +// .split(" ") +// .filter((word) => word.includes("e")) +// .length; +// console.log(countOfWordsContainingEs); + +function countHelper(inputFiles) { + return { + lines: inputFiles.split('\n').length - 1, + words: inputFiles.split(' ').filter(w => w !== "").length, + bytes: inputFiles.length, + }; +} + + +// * `wc -l sample-files/3.txt` +// * `wc -l sample-files/*` +async function countLines(listOfFiles) { + for (const file of listOfFiles) { + const content = await fs.readFile(file, "utf-8"); + + // const linesNumbered = content.split('\n').length-1 + const counts = countHelper(content); + console.log(`${counts.lines} ${file}`) + } +} + +// * `wc -w sample-files/3.txt` +async function countWords(listOfFiles) { + for (const file of listOfFiles) { + const content = await fs.readFile(file, "utf-8"); + + // const wordsCounted = content.split(" ").filter(word => word !== "").length; + // console.log(`${wordsCounted} ${file}`); + const counts = countHelper(content); + console.log(`${counts.words} ${file}`); +} + +} + +// * `wc -c sample-files/3.txt` +async function countBytes(listOfFiles) { + for (const file of listOfFiles) { + const content = await fs.readFile(file, "utf-8"); + // const bytesCounted = content.length; + const counts = countHelper(content); + console.log(`${counts.bytes} ${file}`); + } +} + +// * `wc sample-files/*` +async function countAll(listOfFiles) { + for (const file of listOfFiles) { + const content = await fs.readFile(file, "utf-8"); + const counts = countHelper(content) + + console.log(`${counts.lines} ${counts.words} ${counts.bytes} ${file}`); + } +} + +const argv = process.argv.slice(2); +const files = argv.filter(arg => !arg.startsWith('-')); + +if (argv.includes('-l')) { + await countLines(files); +} else if (argv.includes('-w')) { + await countWords(files); +} else if (argv.includes('-c')) { + await countBytes(files); +} else { + await countAll(files); +} \ No newline at end of file From f8ea2711cad42dc8906e4a7ac6922d4bc6346efc Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Sun, 22 Feb 2026 12:39:04 +0000 Subject: [PATCH 2/4] functions separated and fixed --- .DS_Store | Bin 0 -> 6148 bytes implement-shell-tools/.DS_Store | Bin 0 -> 6148 bytes implement-shell-tools/cat/index.js | 111 ++++++++--------------------- 3 files changed, 29 insertions(+), 82 deletions(-) create mode 100644 .DS_Store create mode 100644 implement-shell-tools/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..616f514c4af5255a392ad8cec735fdea172787b0 GIT binary patch literal 6148 zcmeHKUrWO<5Kp%0GKSCxg*^s*9oWtP_)_Zp0#@`vWwvx^wKlqS_F)YAtY64a;^*-$ zNx|u!MdS{W-(Bu5X@1aL!WiTJH109xFvbKlM2^Z5!QrK@nhi$eI7gJK2=w>pDNU^J zuLFL2joq{7sF*x`{r*Yrx*xn&Z>+2~MN_oJb#SMOAP@8D#1F@pXq_pQMwK2$=gFuT zI_t+O%floajdem2k09jgBFSP^_-dNP6P+6vhiHk`(CN%({exat?(VzuuACkA-LBl( z-kZ-`Vqj}GYY`i%YtA`0mEmOzviJ&UD5@PKfW3TRTfePVEv z4&&0sc@|59CY^D;GR$LFt{yL3uMXo;Ye1{w;+m8gJ#zHkWu1J99;b{fA#9pXHTr9qqp$8|a& OT?8~C)DZ)}z`!Tu??{>e literal 0 HcmV?d00001 diff --git a/implement-shell-tools/.DS_Store b/implement-shell-tools/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..18e59609701195ad7265218325fb4575d8e3eed1 GIT binary patch literal 6148 zcmeHK-AcnS6i&A3GKSC#g#yGaLKs zfZtwc56ov-kbnRFI7+i#?~~W+jjipbXo|MD^PW`hWq!6uNB;ZBS(XhYj%H_!+=)=MB zYSj{Z`v<2Nljry)QE!G$4s2`Lu~@=8D0Vfk-aLs_@(7+XyUZda28aP-fEd_p2JE>Y z+M7)St)3Vl1}Yf9{Xs%QbS)MJ_0|C$UY{}EKtur@-x7$@qHD1*2oVr&N&!tNw@(ai z%E2#fo@=o%Xv!JaGs8G`=JN5v_3Yr6I-POXAoaumF|f`+-IxxZ|L5?_R6g?8OUNPy zh=G5`0B?-F@dy@W&(F$iggRp27Z~^gdv8fw literal 0 HcmV?d00001 diff --git a/implement-shell-tools/cat/index.js b/implement-shell-tools/cat/index.js index cb799d819..feb0fdf79 100644 --- a/implement-shell-tools/cat/index.js +++ b/implement-shell-tools/cat/index.js @@ -1,87 +1,34 @@ -import process from "node:process"; import { promises as fs } from "node:fs"; - -//one for 1 and 3 -async function printOneOrMore(listOfFiles) { - for (const file of listOfFiles) { - const content = await fs.readFile(file, "utf-8"); - console.log(content); - } -} - - -// // 2 * `cat -n sample-files/1.txt` -// async function caseTwo(oneFile) { -// const content = await fs.readFile(oneFile, "utf-8"); -// const separatedToLines = content.split('\n') -// separatedToLines.forEach((line, index) => { -// console.log(`${index + 1} ${line}`) -// }) -// } - -// // 4 * `cat -n sample-files/*.txt` -// async function caseFour(listOfFiles) { -// for (const file of listOfFiles) { -// const content = await fs.readFile(one, "utf-8"); - -// const separatedToLines = content.split('\n') -// separatedToLines.forEach((line, index) => { -// console.log(`${index + 1} ${line}`) -// }) -// } - -// } - -//one instead -// 2 * `cat -n sample-files/1.txt` -// 4 * `cat -n sample-files/*.txt` - -async function caseTwoAndFour(listOfFiles) { - for (const file of listOfFiles) { - const content = await fs.readFile(file, "utf-8"); - - const separatedToLines = content.split('\n') - separatedToLines.forEach((line, index) => { - console.log(`${index + 1} ${line}`) - }) +async function cleanInput(listOfFiles) { + let cleanLinesArr = []; + for (const file of listOfFiles) { + const grabbedText = await fs.readFile(file, "utf-8"); + const splitLines = grabbedText.split("\n"); + cleanLinesArr.push(...splitLines); + } + return cleanLinesArr; } - -} - -// `cat -b sample-files/3.txt` -async function caseFive(listOfFiles) { - for (const file of listOfFiles) { - const content = await fs.readFile(file, "utf-8"); - const separatedToLines = content.split('\n'); - - let countingOnlyFullLines = 1 - separatedToLines.forEach((line, index) => { - if (line !== '') { - console.log(`${countingOnlyFullLines} ${line}`) - countingOnlyFullLines++ - } else { - console.log('') - } - - }) - } +function takeSpecifiedAction(cleanLinesArr, flag) { + let countingOnlyFullLines = 1; + + for (const file of cleanLinesArr) { + // Task: We recommend you start off supporting no flags, then add support for `-n`, then add support for `-b`. + if (!flag) { + console.log(file); + } else if (flag === "-n") { + console.log(`${countingOnlyFullLines} ${file}`); + countingOnlyFullLines += 1; + } else if (flag === "-b") { + if (file === "") { + console.log(""); + } else { + console.log(`${countingOnlyFullLines} ${file}`); + countingOnlyFullLines += 1; + } + } else { + console.log("incorrect flag"); + } + } } - - -const argv = process.argv.slice(2); - -switch (argv[0]) { - case '-n': - await caseTwoAndFour(argv.slice(1)); - break; - - case '-b': - await caseFive(argv.slice(1)); - break; - - default: - await printOneOrMore(argv); - break; -} \ No newline at end of file From 5e484871d8380ab27b719bd302d7728d62e0865d Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Sun, 22 Feb 2026 13:03:30 +0000 Subject: [PATCH 3/4] flag fixed --- implement-shell-tools/cat/index.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/implement-shell-tools/cat/index.js b/implement-shell-tools/cat/index.js index feb0fdf79..86c19f134 100644 --- a/implement-shell-tools/cat/index.js +++ b/implement-shell-tools/cat/index.js @@ -1,15 +1,28 @@ -import { promises as fs } from "node:fs"; +import { readFileSync } from "node:fs"; -async function cleanInput(listOfFiles) { +function cleanInput(listOfFiles) { let cleanLinesArr = []; + for (const file of listOfFiles) { - const grabbedText = await fs.readFile(file, "utf-8"); + const grabbedText = readFileSync(file, "utf-8"); const splitLines = grabbedText.split("\n"); cleanLinesArr.push(...splitLines); } return cleanLinesArr; } +const args = process.argv.slice(2); +let flag; +let restIsFiles; + +if (args[0] && args[0][0] === "-") { + flag = args[0]; + restIsFiles = args.slice(1); +} else { + flag = null; + restIsFiles = args; +} + function takeSpecifiedAction(cleanLinesArr, flag) { let countingOnlyFullLines = 1; @@ -32,3 +45,6 @@ function takeSpecifiedAction(cleanLinesArr, flag) { } } } + +const lines = cleanInput(restIsFiles); +takeSpecifiedAction(lines, flag); From 4fb13cdcfa3877bc6aab627b1e97c832d129caf8 Mon Sep 17 00:00:00 2001 From: katarzynakaz Date: Sun, 22 Feb 2026 13:31:19 +0000 Subject: [PATCH 4/4] wc fixed --- implement-shell-tools/cat/notes.js | 46 ----------------- implement-shell-tools/ls/index.js | 52 ++++++++----------- implement-shell-tools/wc/index.js | 83 ++++++++++++++---------------- 3 files changed, 62 insertions(+), 119 deletions(-) delete mode 100644 implement-shell-tools/cat/notes.js diff --git a/implement-shell-tools/cat/notes.js b/implement-shell-tools/cat/notes.js deleted file mode 100644 index 87f4fd2c1..000000000 --- a/implement-shell-tools/cat/notes.js +++ /dev/null @@ -1,46 +0,0 @@ -import process from "node:process"; -import { promises as fs } from "node:fs"; -// const fs = promises - -// * `cat sample-files/1.txt` -// * `cat -n sample-files/1.txt` -// * `cat sample-files/*.txt` -// * `cat -n sample-files/*.txt` -// * `cat -b sample-files/3.txt` - -// process.argv documentation that process.argv[0] will be the path to node -// process.argv[1] will be the path to this file -// the arguments start at index 2 - -const argv = process.argv.slice(2); -if (argv.length != 1) { - console.error(`Expected exactly 1 argument (a path) to be passed but got ${argv.length}.`); - process.exit(1); -} -const path = argv[0]; - -// const content = await fs.readFile(path, "utf-8"); -// const countOfWordsContainingEs = content -// .split(" ") -// .filter((word) => word.includes("e")) -// .length; -// console.log(countOfWordsContainingEs); - -// `cat sample-files/1.txt` -const content = await fs.readFile('sample-files/1.txt', "utf-8"); -const contentsOfFileOne = content -console.log(contentsOfFileOne); - -// * `cat -n sample-files/1.txt` -const separatedToLines = content.split('\n') -separatedToLines.forEach((line, countLine) => { - countLine+=1 - console.log(line, countLine}) -const withAddedCount = countFileOne. - -// * `cat sample-files/*.txt` - -// * `cat -n sample-files/*.txt` - -// * `cat -b sample-files/3.txt` - diff --git a/implement-shell-tools/ls/index.js b/implement-shell-tools/ls/index.js index 5dc90f6ac..c14cb0e94 100644 --- a/implement-shell-tools/ls/index.js +++ b/implement-shell-tools/ls/index.js @@ -1,49 +1,41 @@ -// fs.readdir import process from "node:process"; import { promises as fs } from "node:fs"; - // `ls -1` async function showAllFilesInDir(directory) { - const listOfFiles = await fs.readdir(directory); - - for (const eachFile of listOfFiles) { - console.log(eachFile); - } + const listOfFiles = await fs.readdir(directory); + + for (const eachFile of listOfFiles) { + if (eachFile[0] !== ".") console.log(eachFile); + } } // `ls -1 sample-files` async function showVisibleInSampleFiles() { - const listOfFiles = await fs.readdir('sample-files'); - - for (const eachFile of listOfFiles) { - if (eachFile[0] !== '.') { - console.log(eachFile); - } - - } -} + const listOfFiles = await fs.readdir("sample-files"); + for (const eachFile of listOfFiles) { + if (eachFile[0] !== ".") { + console.log(eachFile); + } + } +} // `ls -1 -a sample-files` async function showAllInSampleFiles() { - const listOfFiles = await fs.readdir('sample-files'); - - for (const eachFile of listOfFiles) { - - console.log(eachFile); - - } -} - + const listOfFiles = await fs.readdir("sample-files"); + for (const eachFile of listOfFiles) { + console.log(eachFile); + } +} const argv = process.argv.slice(2); -if (argv.includes('-a')) { - await showAllInSampleFiles(); -} else if (argv.includes('sample-files')) { - await showVisibleInSampleFiles(); +if (argv.includes("-a")) { + await showAllInSampleFiles(); +} else if (argv.includes("sample-files")) { + await showVisibleInSampleFiles(); } else { - await showCurrentDir(); + await showAllFilesInDir("."); } diff --git a/implement-shell-tools/wc/index.js b/implement-shell-tools/wc/index.js index 92a366b95..be50589be 100644 --- a/implement-shell-tools/wc/index.js +++ b/implement-shell-tools/wc/index.js @@ -9,69 +9,66 @@ import { promises as fs } from "node:fs"; // .length; // console.log(countOfWordsContainingEs); -function countHelper(inputFiles) { - return { - lines: inputFiles.split('\n').length - 1, - words: inputFiles.split(' ').filter(w => w !== "").length, - bytes: inputFiles.length, - }; +function calculateCounts(inputFiles) { + return { + lines: inputFiles.split("\n").length - 1, + words: inputFiles.split(/\s+/).filter((w) => w !== "").length, + bytes: inputFiles.length, + }; } - // * `wc -l sample-files/3.txt` // * `wc -l sample-files/*` async function countLines(listOfFiles) { - for (const file of listOfFiles) { - const content = await fs.readFile(file, "utf-8"); + for (const file of listOfFiles) { + const content = await fs.readFile(file, "utf-8"); - // const linesNumbered = content.split('\n').length-1 - const counts = countHelper(content); - console.log(`${counts.lines} ${file}`) - } + // const linesNumbered = content.split('\n').length-1 + const counts = calculateCounts(content); + console.log(`${counts.lines} ${file}`); + } } // * `wc -w sample-files/3.txt` async function countWords(listOfFiles) { - for (const file of listOfFiles) { - const content = await fs.readFile(file, "utf-8"); - - // const wordsCounted = content.split(" ").filter(word => word !== "").length; - // console.log(`${wordsCounted} ${file}`); - const counts = countHelper(content); - console.log(`${counts.words} ${file}`); -} - + for (const file of listOfFiles) { + const content = await fs.readFile(file, "utf-8"); + + // const wordsCounted = content.split(" ").filter(word => word !== "").length; + // console.log(`${wordsCounted} ${file}`); + const counts = calculateCounts(content); + console.log(`${counts.words} ${file}`); + } } // * `wc -c sample-files/3.txt` async function countBytes(listOfFiles) { - for (const file of listOfFiles) { - const content = await fs.readFile(file, "utf-8"); - // const bytesCounted = content.length; - const counts = countHelper(content); - console.log(`${counts.bytes} ${file}`); - } + for (const file of listOfFiles) { + const content = await fs.readFile(file, "utf-8"); + // const bytesCounted = content.length; + const counts = calculateCounts(content); + console.log(`${counts.bytes} ${file}`); + } } // * `wc sample-files/*` async function countAll(listOfFiles) { - for (const file of listOfFiles) { - const content = await fs.readFile(file, "utf-8"); - const counts = countHelper(content) - - console.log(`${counts.lines} ${counts.words} ${counts.bytes} ${file}`); - } + for (const file of listOfFiles) { + const content = await fs.readFile(file, "utf-8"); + const counts = calculateCounts(content); + console.log(`${counts.lines} ${counts.words} ${counts.bytes} ${file}`); + } } const argv = process.argv.slice(2); -const files = argv.filter(arg => !arg.startsWith('-')); +const files = argv.filter((arg) => !arg.startsWith("-")); -if (argv.includes('-l')) { - await countLines(files); -} else if (argv.includes('-w')) { - await countWords(files); -} else if (argv.includes('-c')) { - await countBytes(files); +if (argv.includes("-l")) { + await countLines(files); +} else if (argv.includes("-w")) { + await countWords(files); +} else if (argv.includes("-c")) { + await countBytes(files); } else { - await countAll(files); -} \ No newline at end of file + await countAll(files); +}