-
Notifications
You must be signed in to change notification settings - Fork 2
Mirror #12
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
base: main
Are you sure you want to change the base?
Mirror #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| function cat(files, options) { | ||
| let lineNumber = 1; | ||
|
|
||
| files.forEach((file) => { | ||
| const filePath = path.resolve(file); | ||
|
|
||
| try { | ||
| const data = fs.readFileSync(filePath, 'utf8'); | ||
| const lines = data.split('\n'); | ||
|
Comment on lines
+9
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the To "like" or "dislike" this comment, please follow this link |
||
|
|
||
| lines.forEach((line) => { | ||
| if (options.numberNonEmpty && line.trim()) { | ||
| console.log(`${lineNumber}\t${line}`); | ||
| lineNumber++; | ||
| } else if (options.numberLines) { | ||
| console.log(`${lineNumber}\t${line}`); | ||
| lineNumber++; | ||
| } else { | ||
| console.log(line); | ||
| } | ||
|
Comment on lines
+16
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the inner When the same logic lives in multiple branches like this, it can be easy to accidentally change one branch and forget the other if you later tweak the formatting (for example, changing the delimiter between the number and the text, or adjusting how the number is displayed). That kind of small divergence can create subtle inconsistencies that are tricky to notice. How might you restructure this part so that the decision of whether to number the line is separate from how you number a line, so the numbering behavior itself only lives in one place? To "like" or "dislike" this comment, please follow this link |
||
| }); | ||
| } catch (err) { | ||
| console.error(`cat: ${file}: No such file or directory`); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function main() { | ||
| const args = process.argv.slice(2); | ||
| const options = { | ||
| numberLines: false, | ||
| numberNonEmpty: false, | ||
| }; | ||
|
|
||
| const files = []; | ||
|
|
||
| args.forEach((arg) => { | ||
| if (arg === '-n') { | ||
| options.numberLines = true; | ||
| } else if (arg === '-b') { | ||
| options.numberNonEmpty = true; | ||
| } else { | ||
| files.push(arg); | ||
| } | ||
| }); | ||
|
|
||
| if (files.length === 0) { | ||
| console.error('Usage: node cat.js [-n | -b] <file>...'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| cat(files, options); | ||
| } | ||
|
|
||
| main(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| function listFiles(directory, options) { | ||
| try { | ||
| const files = fs.readdirSync(directory, { withFileTypes: true }); | ||
|
|
||
| files.forEach((file) => { | ||
| if (!options.all && file.name.startsWith('.')) { | ||
| return; // Skip hidden files unless -a is specified | ||
| } | ||
|
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On this line you explain that hidden files are being skipped unless To "like" or "dislike" this comment, please follow this link
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On this line the comment You might ask yourself: if you renamed To "like" or "dislike" this comment, please follow this link
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On this line, the inline comment How might you make the code expressive enough that this comment isn’t needed at all? For example, could naming To "like" or "dislike" this comment, please follow this link |
||
| console.log(file.name); | ||
| }); | ||
| } catch (err) { | ||
| console.error(`ls: cannot access '${directory}': No such file or directory`); | ||
| } | ||
| } | ||
|
|
||
| function main() { | ||
| const args = process.argv.slice(2); | ||
| const options = { | ||
| all: false, | ||
| }; | ||
|
|
||
| let directories = ['.']; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable name When names imply different capabilities than what the code actually does, it can make it harder to extend or debug later because future changes may be based on incorrect assumptions. How might you rename the variable or adjust the logic so that the name clearly reflects that, as written, only a single directory is ever used? To "like" or "dislike" this comment, please follow this link |
||
|
|
||
| args.forEach((arg) => { | ||
| if (arg === '-1') { | ||
| // -1 is the default behavior, so no action needed | ||
|
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, the comment When you see yourself adding comments like this, it can help to ask: If I removed this comment, would the code still be understandable to someone familiar with JavaScript and CLI tools? If the answer is yes, it might be better to omit the comment and let the code speak for itself. To "like" or "dislike" this comment, please follow this link |
||
| } else if (arg === '-a') { | ||
|
Comment on lines
+30
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment explains that To "like" or "dislike" this comment, please follow this link
Comment on lines
+30
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here the comment When you see yourself adding a comment to explain that a branch does nothing, it can be a good moment to consider whether the code itself could make that intent clear. For example, could a small refactor (like parsing options separately, or documenting supported flags in help text) make this comment unnecessary, while still making it obvious that To "like" or "dislike" this comment, please follow this link |
||
| options.all = true; | ||
| } else { | ||
| directories = [arg]; | ||
| } | ||
| }); | ||
|
Comment on lines
+27
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In How might you rename this variable (or adjust how it’s used) so that another reader can immediately see whether multiple directories are really supported or not? Aligning the name with the actual behavior can make it easier for future you (or others) to understand the supported usage just from reading the code. To "like" or "dislike" this comment, please follow this link |
||
|
|
||
| directories.forEach((directory) => { | ||
| listFiles(directory, options); | ||
| }); | ||
| } | ||
|
|
||
| main(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| function countFile(filePath, options) { | ||
| try { | ||
| const data = fs.readFileSync(filePath, 'utf8'); | ||
|
|
||
| const lines = data.split('\n').length; | ||
| const words = data.split(/\s+/).filter(Boolean).length; | ||
| const bytes = Buffer.byteLength(data, 'utf8'); | ||
|
|
||
| if (options.lines) { | ||
| console.log(`${lines}\t${filePath}`); | ||
| } else if (options.words) { | ||
| console.log(`${words}\t${filePath}`); | ||
| } else if (options.bytes) { | ||
| console.log(`${bytes}\t${filePath}`); | ||
| } else { | ||
| console.log(`${lines}\t${words}\t${bytes}\t${filePath}`); | ||
| } | ||
| } catch (err) { | ||
| console.error(`wc: ${filePath}: No such file or directory`); | ||
| } | ||
| } | ||
|
|
||
| function main() { | ||
| const args = process.argv.slice(2); | ||
| const options = { | ||
| lines: false, | ||
| words: false, | ||
| bytes: false, | ||
| }; | ||
|
|
||
| const files = []; | ||
|
|
||
| args.forEach((arg) => { | ||
| if (arg === '-l') { | ||
| options.lines = true; | ||
| } else if (arg === '-w') { | ||
| options.words = true; | ||
| } else if (arg === '-c') { | ||
| options.bytes = true; | ||
| } else { | ||
| files.push(arg); | ||
| } | ||
| }); | ||
|
|
||
| if (files.length === 0) { | ||
| console.error('Usage: wc [-l | -w | -c] <file>...'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| files.forEach((file) => { | ||
| const filePath = path.resolve(file); | ||
| countFile(filePath, options); | ||
|
Comment on lines
+55
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the You might ask yourself: would calling To "like" or "dislike" this comment, please follow this link
Comment on lines
+55
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In To "like" or "dislike" this comment, please follow this link |
||
| }); | ||
| } | ||
|
|
||
| main(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
catfunction,filePathis created as a separate variable and then only used immediately once in thereadFileSynccall (and not reused elsewhere, for example in the error message). When a temporary variable is only used once right after it’s declared, it can be a hint that you might not need to name it at all.One thing to think about: would inlining
path.resolve(file)directly intofs.readFileSynckeep the code just as readable for you, while slightly reducing the amount of state you have to track in your head? Or do you feel that havingfilePathnamed explicitly helps you understand what’s going on? Getting into the habit of asking yourself this question for single-use temporaries can help you find a good balance between clarity and conciseness.To "like" or "dislike" this comment, please follow this link