London| 2026-MAR-SDC | Imran Mohamed | Sprint 3 | Implement shell tools#450
London| 2026-MAR-SDC | Imran Mohamed | Sprint 3 | Implement shell tools#450i786m wants to merge 5 commits into
Conversation
SlideGauge
left a comment
There was a problem hiding this comment.
There are some notes from my side, could you fix them please?
| const spacer = ' '; | ||
|
|
||
| for (const path of argv) { | ||
| const content = await fs.readFile(path, 'utf-8'); |
| } | ||
| for (const line of lines) { | ||
| if (showLineNumbers) { | ||
| process.stdout.write(`${spacer} ${lineNumber++} ${line}\n`); |
There was a problem hiding this comment.
real cat places tab between numbers and line, and also aligns line number to the length of the number.
| for (const line of lines) { | ||
| if (showLineNumbers) { | ||
| process.stdout.write(`${spacer} ${lineNumber++} ${line}\n`); | ||
| } else if (showNonEmptyLineNumbers && line.trim() !== '') { |
There was a problem hiding this comment.
Real cat -b considers only truly empty lines (zero characters) as blank, not whitespace-only lines.
| for (const path of argv) { | ||
| const content = await fs.readFile(path, 'utf-8'); | ||
| const lines = content.split('\n'); | ||
| while (lines.length > 0 && lines[lines.length - 1] === '') { |
There was a problem hiding this comment.
while pops all trailing blank lines, not just one. A file intentionally ending with multiple blank lines will have them silently removed.
| process.stdout.write(`${line}\n`); | ||
| } | ||
| } | ||
| } No newline at end of file |
| program | ||
| .name('wc') | ||
| .description('Counts the number of lines, words, and characters in a file.') | ||
| .argument('<path>', 'The path to the file to analyze') |
There was a problem hiding this comment.
Real wc accepts multiple files and prints a total row.
|
|
||
| const content = await fs.readFile(path, 'utf-8'); | ||
|
|
||
| const lineCount = content.split('\n').filter(Boolean).length; |
There was a problem hiding this comment.
File with a blank line in the middle will have it excluded from the count.
| const content = await fs.readFile(path, 'utf-8'); | ||
|
|
||
| const lineCount = content.split('\n').filter(Boolean).length; | ||
| const wordCount = content.split(' ').filter(Boolean).length; |
There was a problem hiding this comment.
Only splits on spaces — tabs and newlines between words are not treated as separators.
|
|
||
| const lineCount = content.split('\n').filter(Boolean).length; | ||
| const wordCount = content.split(' ').filter(Boolean).length; | ||
| const characterCount = content.length; |
There was a problem hiding this comment.
content.length counts UTF-16 code units. Real wc -c counts bytes.
| const lineCount = content.split('\n').filter(Boolean).length; | ||
| const wordCount = content.split(' ').filter(Boolean).length; | ||
| const characterCount = content.length; | ||
| console.log(` ${showLines ? lineCount : ''} ${showWords ? wordCount : ''} ${showCharacters ? characterCount : ''} ${path}`); No newline at end of file |
There was a problem hiding this comment.
What happens if some flags are missing? Does it affect the format?
Self checklist
Changelist
Implementation of ls, wc and cat command line tools in node.js