generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
London | 25-SDC-July | Franklin Kamela | Sprint 3 | Feature/Implement Shell Tools #135
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
Open
Fradoka
wants to merge
15
commits into
CodeYourFuture:main
Choose a base branch
from
Fradoka:feature/implement-Shell-Tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0611383
exercise ls
Fradoka 1f5b567
exercise cat
Fradoka 0a71087
exercise wc
Fradoka 72271cf
exercise grep
Fradoka 0d3bc17
exercise sed
Fradoka 22a04bb
exercise awk
Fradoka 32a3a87
exercise number systems
Fradoka 4ef88e2
Revert mistaken commits from main branch
Fradoka 4e42980
implemented ls
Fradoka 647f171
implemented cat and wc
Fradoka e1a0f74
updated my_cat.js
Fradoka a83f327
updated my_ls.js
Fradoka 7f7d707
updated my_wc.js
Fradoka a38f2e9
updated my_cat.js to address numbering for multiple files
Fradoka 6098359
amended my_cat.js to address review on counter variable
Fradoka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { promises as fs } from 'node:fs'; | ||
| import { argv } from 'node:process'; | ||
|
|
||
| const args = argv.slice(2); // skip node and script name | ||
|
|
||
| let showLineNumb = false; | ||
| let showNoBlankNumb = false | ||
| const files = []; | ||
|
|
||
|
|
||
| args.forEach(arg => { | ||
| if (arg === '-n') { | ||
| showLineNumb = true; | ||
| } else if (arg === '-b') { | ||
| showNoBlankNumb = true; | ||
| }else { | ||
| files.push(arg); | ||
| } | ||
| }); | ||
|
|
||
| if (files.length === 0) { | ||
| console.error('No input file specified'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| for (const file of files) { | ||
| try { | ||
| let content = await fs.readFile(file, 'utf-8'); | ||
| const lines = content.split('\n'); | ||
|
|
||
| if (showNoBlankNumb) { // Number only non-blank lines | ||
| let counter = 1; | ||
| content = lines.map(line => | ||
| line.trim() === '' ? '' : `${counter++}\t${line}` | ||
| ).join('\n'); | ||
| } else if (showLineNumb) { // Number all lines | ||
| content = lines.map((line, i) => `${i + 1}\t${line}`).join('\n'); | ||
| } | ||
| console.log(content); | ||
| } catch (err) { | ||
| console.error(`Cannot access '${file}': ${err.message}`); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { readdirSync } from 'node:fs'; | ||
| import { argv } from 'node:process'; | ||
|
|
||
| const args = argv.slice(2); // skips node and script name | ||
|
|
||
| const flags = args.filter(arg => arg.startsWith('-')); | ||
| const operands = args.filter(arg => !arg.startsWith('-')); | ||
|
|
||
| let showAll = flags.includes('-a'); | ||
| let onePerLine = flags.includes('-1'); | ||
|
|
||
| if (operands.length === 0) operands.push('.'); // defaults to current dir | ||
|
|
||
| operands.forEach(dir => { | ||
| try { | ||
| let files = readdirSync(dir); // Reads the contents of the dir synchronously (readdirSync) | ||
|
|
||
| if (!showAll) { | ||
| files = files.filter(file => !file.startsWith('.')); | ||
| } | ||
|
|
||
| files.sort((a, b) => a.localeCompare(b)); | ||
|
|
||
| if (onePerLine) { | ||
| files.forEach(file => console.log(file)); | ||
| } else { | ||
| console.log(files.join(' ')); | ||
| } | ||
| } catch (err) { | ||
| console.error(`ls: cannot access '${dir}': ${err.message}`); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { promises as fs } from 'node:fs'; | ||
| import { argv } from 'node:process'; | ||
|
|
||
| const args = argv.slice(2); | ||
|
|
||
| if (args.length === 0) { | ||
| console.error('Usage: node my_wc.mjs [-l] [-w] [-c] <file> [file2 ...]'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // Parse flags and files | ||
| const flags = { | ||
| l: false, | ||
| w: false, | ||
| c: false, | ||
| }; | ||
|
|
||
| const files = []; | ||
|
|
||
| for (const arg of args) { | ||
| if (arg.startsWith('-') && arg.length > 1) { | ||
| for (const ch of arg.slice(1)) { | ||
| if (flags.hasOwnProperty(ch)) { | ||
| flags[ch] = true; | ||
| } else { | ||
| console.error(`Unknown option: -${ch}`); | ||
| process.exit(1); | ||
| } | ||
| } | ||
| } else { | ||
| files.push(arg); | ||
| } | ||
| } | ||
|
|
||
| if (files.length === 0) { | ||
| console.error('Error: no files specified.'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // If no flags specified, show all by default | ||
| const showLines = flags.l || (!flags.l && !flags.w && !flags.c); | ||
| const showWords = flags.w || (!flags.l && !flags.w && !flags.c); | ||
| const showBytes = flags.c || (!flags.l && !flags.w && !flags.c); | ||
|
|
||
| function pad(num, width = 8) { | ||
| return num.toString().padStart(width, ' '); | ||
| } | ||
|
|
||
| function formatOutput({ lines, words, bytes }, label) { | ||
| let output = ''; | ||
| if (showLines) output += pad(lines); | ||
| if (showWords) output += pad(words); | ||
| if (showBytes) output += pad(bytes); | ||
| output += ` ${label}`; | ||
| return output; | ||
| } | ||
|
|
||
| async function countFile(file) { | ||
| try { | ||
| const content = await fs.readFile(file, 'utf-8'); | ||
| const lines = content.split('\n').length - 1; | ||
| const words = content.trim().split(/\s+/).filter(Boolean).length; | ||
| const bytes = Buffer.byteLength(content, 'utf-8'); | ||
|
|
||
| const counts = { lines, words, bytes }; | ||
| console.log(formatOutput(counts, file)); | ||
|
|
||
| return counts; | ||
| } catch (err) { | ||
| console.error(`Cannot access '${file}': ${err.message}`); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| async function main() { | ||
| // Instead of .map, I can explicitly collect promises using forEach(): | ||
| const promises = []; | ||
| files.forEach(file => { | ||
| promises.push(countFile(file)); | ||
| }); | ||
| const counts = await Promise.all(promises); | ||
|
|
||
| const validCounts = counts.filter(c => c !== null); | ||
|
|
||
| if (validCounts.length > 1) { | ||
| const totals = { | ||
| lines: validCounts.reduce((sum, c) => sum + c.lines, 0), | ||
| words: validCounts.reduce((sum, c) => sum + c.words, 0), | ||
| bytes: validCounts.reduce((sum, c) => sum + c.bytes, 0), | ||
| } | ||
|
|
||
| console.log(formatOutput(totals, 'total')); | ||
| } | ||
| } | ||
|
|
||
| main(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Try testing your updated solution with the numbering options on with multiple files now your app supports it. How would you expect it to work? If you compare your solution to the original cat, can you see a discrepancy?