Skip to content

LONDON | DONARA BLANC | MODULE TOOLS | SPRINT -3 #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

donarbl
Copy link

@donarbl donarbl commented Apr 1, 2025

Self checklist

  • [ x] I have committed my files one by one, on purpose, and for a reason
  • [ x] I have titled my PR with COHORT_NAME | FIRST_NAME LAST_NAME | REPO_NAME | WEEK
  • [ x] I have tested my changes
  • [ x] My changes follow the style guide
  • [ x] My changes meet the requirements of this task

Changelist

I've used libraries and implemented functions that work with cat, ls and wc and associated flags.

Questions

Ask any questions you have for your reviewer.

@donarbl donarbl added Needs Review Participant to add when requesting review 📅 Sprint 3 Assigned during Sprint 3 of this module labels Apr 1, 2025
Copy link

@blorente blorente left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR! I'm Borja, a volunteer, and I'll be reviewing your code.

The implementations of cat and ls are good! I have left a few edge cases that are not covered, as well as a few suggestions, but you're definitely on the right track :)

The implementation of wc is missing a few parts:

  • How could we handle being given several files? Like this case given in the problem description: wc sample-files/*
  • How could we handle the following flags: -l, -c, and -w?

Lastly, do we need the .DS_Store files?

Keep up the good work!

function printLines(lines) {
lines.forEach((line) => {
if (bFlag && line.trim()) {
console.log(`${nonEmptyLineNum++} ${line}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be something wrong with the output. When I run this code, the lines on multiple files are counted the with the same counter:

$ node cat.js -b sample-files/*.txt
1 Once upon a time...
2 There was a house made of gingerbread.
3 It looked delicious.
4 I was tempted to take a bite of it.
5 But this seemed like a bad idea...

6 There's more to come, though...

However, when we run cat, we can see that every line number is counted individually:

➜ cat -b sample-files/*.txt
     1  Once upon a time...  # From sample-files/1.txt
     1  There was a house made of gingerbread.   # From sample-files/2.txt
     1  It looked delicious.   # From sample-files/3.txt
     2  I was tempted to take a bite of it.
     3  But this seemed like a bad idea...

     4  There's more to come, though...

How can we modify the code so that the lines are counted correctly?

@@ -0,0 +1,45 @@

const { promises: fs } = require("fs");// it reads the files
const process = require("process");// gets large files information

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment accurate? The only use of process I can find is process.argv, which doesn't match the comment.


const { promises: fs } = require("fs");// it reads the files
const process = require("process");// gets large files information
const { program } = require("commander");// it puts out in chunks or parses information

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment accurate? Is there a way you could shape the program that would codify this intention in the code (e.g. with a function name or a variable name), instead of relying on a comment?

Comment on lines +17 to +18
let lineNum = 1; // Counting for -n flag
let nonEmptyLineNum = 1; // Counting for -b flag

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments can get out of date. Is there a way you can codify this intention in the code? e.g. via variable name.

}
} catch (err) {
// Enhanced error handling to print ls behavior
console.error(`ls: cannot access '${dir}': No such file or directory`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be other reasons why you can't access the directory. For instance, you may not have read access to it (For instance, if you get this output: Permission error: EACCES: permission denied, scandir ‘/restricted’).

How should we handle those cases?

program
.name("ls")
.description("An implementation of the ls command")
.option("-a, --all", "Include hidden files")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When -a is set, it should also print the current directory (.) and the parent diractory (..).

➜ ls -1 -a sample-files
.
..
.hidden.txt
1.txt
2.txt
3.txt
dir

How could we modify this solution so that we do print them?

.option("-a, --all", "Include hidden files")
.option("-1", "List one file per line") // Support for -1 flag
.argument("[directory]", "The directory to list", ".") // Default directory is "."
.parse(process.argv);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when we receive more than one argument? (e.g. node ls.js directory1 directory2)

files.forEach(file => console.log(file));
} else {
// Default behavior: print files space-separated (like ls without -1)
console.log(files.join(" "));
Copy link

@blorente blorente May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ls, files are printed with a certain amount of padding, so that if we have many files, they appear to be in a table:

➜ /bin/ls
ls.js                   package-lock.json           README.md
node_modules            package.json                sample-files

This is not part of the assignment brief but, as a streacth goal, would you like to consider how to pad the outputs like that?

@blorente blorente added 👀 Review Git Changes requested to do with Git 👀 Review Requirements Changes requested to meet requirements Reviewed Volunteer to add when completing a review and removed Needs Review Participant to add when requesting review labels May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👀 Review Git Changes requested to do with Git 👀 Review Requirements Changes requested to meet requirements Reviewed Volunteer to add when completing a review 📅 Sprint 3 Assigned during Sprint 3 of this module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants