Skip to content

Commit a38f2e9

Browse files
committed
updated my_cat.js to address numbering for multiple files
1 parent 7f7d707 commit a38f2e9

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

implement-shell-tools/cat/my_cat.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,21 @@ if (files.length === 0) {
2323
process.exit(1);
2424
}
2525

26+
// Global counters across all files
27+
let lineCounter = 1;
28+
let nonBlankCounter = 1;
29+
2630
for (const file of files) {
2731
try {
2832
let content = await fs.readFile(file, 'utf-8');
2933
const lines = content.split('\n');
3034

3135
if (showNoBlankNumb) { // Number only non-blank lines
32-
let counter = 1;
3336
content = lines.map(line =>
34-
line.trim() === '' ? '' : `${counter++}\t${line}`
37+
line.trim() === '' ? '' : `${nonBlankCounter++}\t${line}`
3538
).join('\n');
3639
} else if (showLineNumb) { // Number all lines
37-
content = lines.map((line, i) => `${i + 1}\t${line}`).join('\n');
40+
content = lines.map((line) => `${lineCounter++}\t${line}`).join('\n');
3841
}
3942
console.log(content);
4043
} catch (err) {

0 commit comments

Comments
 (0)