ZA | 25-SDC-July | Luke Manyamazi | Sprint 3 | Implement Shell Tools Exercises#132
Conversation
LonMcGregor
left a comment
There was a problem hiding this comment.
Good work on this, I've left some comments for where you could start to improve more.
For your questions, yes, it looks like you're handling defaults for wc fine. I think you are handling output fine.
If you are thinking of handling more edge cases, think about how people might use these commands - what sort of arguments might get passed to your code?
implement-shell-tools/cat/cat.mjs
Outdated
| const lines = content.split('\n'); | ||
|
|
||
| for (const line of lines) { | ||
| const shouldNumber = (numberAllLines && !numNotBlank) || (numNotBlank && line.trim() !== ''); |
There was a problem hiding this comment.
You are not numbering non-blank lines when -b is used. Can you see why that is?
implement-shell-tools/ls/ls.mjs
Outdated
| .name("ls") | ||
| .description("Lists the files in a directory") | ||
| .option("-1, --one", "One per line") | ||
| .option("-a", "Include files starting with dot") |
There was a problem hiding this comment.
You offer -a as an option, but is this ever used?
There was a problem hiding this comment.
Throughout this file there is quite a lot of duplication. Can you think of how you might reduce this?
- cat.mjs: - Fixed -b option to number only non-blank lines. - Unified line number padding to avoid repetition. - Added error handling for directories and missing files. - ls.mjs: - Correctly implemented -a option to include hidden files. - Validates single directory argument. - Sorted output and handled one-column listing. - wc.mjs: - Centralized line, word, and character counting in countFile(). - Defaults to counting all metrics if no options are given. - Added support for multiple files with totals. - Improved error handling for missing files and directories. Overall: - Removed duplication, clarified logic, and improved alignment with standard Unix behavior.
|
Hi @LonMcGregor, Thanks for your feedback! Here’s a summary of the changes and improvements made to cat.mjs, ls.mjs, and wc.mjs: 1️⃣ cat.mjs Fixed numbering logic for -b (number non-blank lines). Previously, non-blank lines weren’t consistently numbered. 2️⃣ ls.mjs 3️⃣ wc.mjs |
LonMcGregor
left a comment
There was a problem hiding this comment.
Thanks for making these changes. cat and ls look good now. I still notice some duplication in the wc file however.
Have a look at the last 2 blocks of code. They're both doing the same thing: Check options, push to an array,add padding, then print out. Is there a way you could abstract that code a bit?
… counts into `formatCounts()` helper - Precompute active count options in `activeCounts` to remove repeated ternaries - Simplify per-file and total output logic for readability and maintainability
|
Yes, I can abstract that repetition into a helper function, e.g. formatCounts(result, options) that returns the formatted count string for a given result object. Check my implementation. |
LonMcGregor
left a comment
There was a problem hiding this comment.
Looks great! You're finished with this task now
Learners, PR Template
Self checklist
Changelist
This PR includes implementations of basic Unix-like command-line tools in Node.js:
ls: Lists files in a directory.
cat: Reads and prints the contents of one or more files.
wc: Counts lines, words, and characters in one or more files, with support for flags (-l, -w, -c).
Each command was tested to match expected output behavior for the given use cases.
Questions