-
Notifications
You must be signed in to change notification settings - Fork 2
Mirror of 437 #6
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
4350f48
276fc2b
8675a8b
99cb233
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,40 @@ | ||
| import process from "node:process"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| // THis will give an array without the path to node and to the file. | ||
| const argv = process.argv.slice(2); | ||
|
|
||
| //Get line numbers. | ||
| const showNumbers = argv.includes("-n"); | ||
| const showNonBlankNumbers = argv.includes("-b"); | ||
|
|
||
| //filter the - from the array argv as it's a flag. | ||
| const filePaths = argv.filter((arg) => !arg.startsWith("-")); | ||
| let counterLines = 1; | ||
|
|
||
| for (const path of filePaths) { | ||
| try { | ||
| const content = await fs.readFile(path, "utf-8"); | ||
|
|
||
| //split the text at the new line character. | ||
| const splitLines = content.split("\n"); | ||
|
|
||
| splitLines.forEach((line) => { | ||
| if (showNumbers) { | ||
| console.log(`${counterLines++} ${line}`); | ||
| } else if (showNonBlankNumbers) { | ||
| // increment and show numbers only if the line is not empty. | ||
| if (line.trim() !== "") { | ||
| console.log(`${counterLines++} ${line}`); | ||
| } else { | ||
| // print empty lines | ||
| console.log(line); | ||
| } | ||
| } else { | ||
| console.log(line); | ||
| } | ||
|
Comment on lines
+23
to
+35
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. I've noticed that the logic for printing lines with or without numbers is repeated in a few places inside your forEach loop. If you ever wanted to change how lines are printed, you'd have to update it in multiple spots. Can you think of a way to group this logic into a function, so you only have to update it in one place if you want to change how lines are displayed? |
||
| }); | ||
| } catch (error) { | ||
| console.log(`Could not read: ${path}`); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import fs from "node:fs"; | ||
| import process from "node:process"; | ||
|
|
||
| // This will give an array without the path to node and to the file. | ||
| const argv = process.argv.slice(2); | ||
|
|
||
| // filter the flag to find the target folder. | ||
| const filePaths = argv.filter((arg) => !arg.startsWith("-")); | ||
| const showHiddenFiles = argv.includes("-a"); | ||
|
|
||
| // if no folder provide we use the current one | ||
| const target = filePaths[0] || "."; | ||
| // read the file. | ||
| const files = fs.readdirSync(target); | ||
|
|
||
| // save the result into the variable. | ||
| let filteredFIles = files; | ||
| if (!showHiddenFiles) { | ||
| filteredFIles = files.filter((file) => !file.startsWith(".")); | ||
| } else { | ||
| // we use spread operator to merge the paths. | ||
| filteredFIles = [".", "..", ...files]; | ||
| } | ||
|
|
||
| // Print using -1 . | ||
| filteredFIles.forEach((file) => { | ||
| console.log(file); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import process, { exit } from "node:process"; | ||
| import fs from "node:fs"; | ||
|
|
||
| const argv = process.argv.slice(2); | ||
|
|
||
| const showWords = argv.includes("-w"); | ||
| const showLines = argv.includes("-l"); | ||
| const showBytes = argv.includes("-c"); | ||
| const showCharacters = argv.includes("-m"); | ||
|
|
||
| // filter flags, and getting the string of filename | ||
| const filePaths = argv.filter((arg) => !arg.startsWith("-")); | ||
|
|
||
| const noFlags = !showLines && !showCharacters && !showWords && !showCharacters; | ||
| if (!filePaths) { | ||
| console.error("PLease provide a file path"); | ||
| process.exit(1); | ||
| } | ||
| // loop trough the array to get each file path. | ||
| filePaths.forEach((filePath) => { | ||
| const content = fs.readFileSync(filePath, "utf-8"); | ||
|
|
||
| const lines = content.split("\n").length - 1; | ||
| const words = content | ||
| .trim() | ||
| .split(/\s+/) | ||
| .filter((word) => word != "").length; | ||
| // here I used Buffer.byteLength even if characters and bytes can be the same number .length, however sometimes an emoji or special characters can be heavier 2b or 4b | ||
| const bytes = Buffer.byteLength(content); | ||
| const characters = content.length; | ||
|
Comment on lines
+23
to
+30
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 logic for counting lines, words, bytes, and characters is written inside the forEach loop. If you wanted to use this logic elsewhere, you might have to copy it. Can you think of a way to move this logic into a function that takes the file content and returns these counts?
Member
Author
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 seems quite speculative again? |
||
|
|
||
| let output = ""; | ||
|
|
||
| if (showLines || noFlags) output += `${lines} `; | ||
| if (showWords || noFlags) output += `${words} `; | ||
| if (showBytes || noFlags) output += `${bytes} `; | ||
| if (showCharacters || noFlags) output += `${characters} `; | ||
|
|
||
| console.log(`${output} ${filePath}`); | ||
| }); | ||
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 'counterLines' is declared outside your main file loop and is used for numbering lines across all files. Do you want the line numbers to continue across files, or should they reset for each file? If you want them to reset, where could you declare 'counterLines' so it starts from 1 for each file?
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.
As other PRs