-
Notifications
You must be signed in to change notification settings - Fork 2
Mirror of 408 #8
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?
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; | ||
|
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 'lineNumber' is used to number lines, but its behavior changes depending on the options. For example, with '-b', it only numbers non-empty lines, but the name doesn't reflect this conditional behavior. When naming variables, it's helpful to consider if the name always matches its use, especially when options change how it's incremented. 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 'lineNumber' is used to number lines, but its behavior changes depending on the options. For example, with '-b', it only numbers non-empty lines, but the name doesn't reflect this conditional behavior. When naming variables, it's helpful to consider if the name always matches its use, especially when options change how it's incremented. |
||
|
|
||
| files.forEach((file) => { | ||
| const filePath = path.resolve(file); | ||
|
|
||
| try { | ||
| const data = fs.readFileSync(filePath, 'utf8'); | ||
| const lines = data.split('\n'); | ||
|
|
||
| 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); | ||
| } | ||
| }); | ||
| } 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 | ||
|
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 line 12, there's a comment: 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 line 12, there's a comment: 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 line 12, there's a comment: 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 line 12, there's a comment: 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 line 12, you have a comment: |
||
| } | ||
|
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 line 31, there's a comment: |
||
| 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 = ['.']; | ||
|
|
||
| args.forEach((arg) => { | ||
| if (arg === '-1') { | ||
| // -1 is the default behavior, so no action needed | ||
|
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 line 31, there's a comment: 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 line 31, there's a comment: 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 line 31, there's a comment: 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 line 31, there's a comment: 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 line 31, there's a comment: |
||
| } else if (arg === '-a') { | ||
|
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 line 31, there's a comment: |
||
| 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. The variable 'directories' is named as if it could hold multiple directories, but your code only ever allows one directory to be listed at a time. This could be confusing to someone reading your code, as they might expect it to handle multiple directories. How could you name this variable to better reflect its purpose and avoid confusion? |
||
|
|
||
| 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); | ||
| }); | ||
| } | ||
|
|
||
| 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.
The variable 'lineNumber' is used to number lines, but its behavior changes depending on the options. For example, with '-b', it only numbers non-empty lines, but the name doesn't reflect this conditional behavior. When naming variables, it's helpful to consider if the name always matches its use, especially when options change how it's incremented.