-
-
Notifications
You must be signed in to change notification settings - Fork 92
London | 26-SDC-Mar | Khilola Rustamova| Sprint 3 |implementing shell tools #485
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 2 commits
3cb1e27
5ede2e8
82f5cc2
bcc13af
6d08c59
d0e165c
1b14d59
2b331b7
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,41 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| const fs = require("fs"); | ||
|
|
||
| const args = process.argv.slice(2); | ||
|
|
||
| let numberLines = false; | ||
| let numberNonEmpty = false; | ||
|
|
||
| if (args[0] === "-n") { | ||
| numberLines = true; | ||
| args.shift(); | ||
| } else if (args[0] === "-b") { | ||
| numberNonEmpty = true; | ||
| args.shift(); | ||
| } | ||
| let count = 1; | ||
|
|
||
| args.forEach((file) => { | ||
| const content = fs.readFileSync(file, "utf-8"); | ||
| const lines = content.split(/\r?\n/); | ||
|
|
||
|
|
||
| if (numberLines){ | ||
|
Member
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 code is a bit repetitive - in both the Can you think how to avoid this, so we can see more easily what's the same between each branch, and what the differences are? |
||
| lines.forEach((line) => { | ||
| console.log(`${count} ${line}`); | ||
| count++; | ||
| }); | ||
| } else if (numberNonEmpty){ | ||
| lines.forEach((line) => { | ||
| if (line !== "") { | ||
| console.log(`${count} ${line}`); | ||
| count++; | ||
| } else { | ||
| console.log(""); | ||
| } | ||
| }); | ||
| } else { | ||
| console.log(content); | ||
| } | ||
|
Member
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. These lines aren't indented correctly |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| number_lines=false | ||
| number_non_empty=false | ||
|
|
||
| # check for flags | ||
| if [ "${1:-}" = "-n" ]; then | ||
| number_lines=true | ||
| shift | ||
| elif [ "${1:-}" = "-b" ]; then | ||
| number_non_empty=true | ||
| shift | ||
| fi | ||
|
|
||
| count=1 | ||
|
|
||
| # Loop through all files | ||
|
|
||
| for file in "$@" | ||
| do | ||
| while IFS= read -r line | ||
| do | ||
| if [ "$number_lines" = true ]; then | ||
| echo "$count $line" | ||
| count=$((count+1)) | ||
|
|
||
| else if [ "$number_non_empty" = true ]; then | ||
| if [ -n "$line" ]; then | ||
| echo "$count $line" | ||
| count=$((count+1)) | ||
| else | ||
| echo "" | ||
| fi | ||
|
|
||
| else | ||
| echo "$line" | ||
| fi | ||
| fi | ||
| done < "$file" | ||
| done |
|
Member
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. When I ran
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. My current implementation uses fs.readdirSync(), which returns directory entries in a flat list without guaranteeing the same ordering semantics as Unix ls. fs.readdirSync() provides a higher-level abstraction than the Unix ls syscall, and does not include . and ... Therefore, matching ls -a exactly requires explicitly simulating these entries and enforcing ordering rules, rather than relying purely on the filesystem API output. ls -a does not treat hidden files separately — it includes them in the same sorted output as normal files, with . and .. always appearing first. Because of this, to match ls exactly, I would need to explicitly enforce the same ordering rules: sorting the full dataset uniformly and then applying special placement for . and ... Without that, the ordering (e.g. hidden files appearing first or in different positions) can differ from real ls. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| const fs = require("node:fs"); | ||
| const path = require("node:path"); | ||
|
|
||
| function parseArgs(args) { | ||
| let showAll = false; | ||
| let targetDir = "."; | ||
|
|
||
| for (const arg of args) { | ||
| if (arg === "-a") { | ||
| showAll = true; | ||
| } else if (!arg.startsWith("-")) { | ||
| targetDir = arg; | ||
| } | ||
|
Member
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. If I run this program like: what will you do with the arguments
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. In my old implementation, -b and -c would be ignored because they are not explicitly handled and are treated as unknown flags. This is not ideal from a user experience perspective, because the program silently ignores input that the user may expect to have an effect. A better approach would be to either warn the user about unknown flags or throw an error, so that invalid or unsupported options are explicitly surfaced rather than ignored. |
||
| } | ||
|
|
||
| return { showAll, targetDir }; | ||
| } | ||
|
|
||
| function listDirectory(dirPath, showAll) { | ||
| let files = fs.readdirSync(dirPath); | ||
|
|
||
| if (!showAll) { | ||
| files = files.filter(file => !file.startsWith(".")); | ||
| } | ||
|
|
||
| return files.sort(); | ||
| } | ||
|
|
||
| function main() { | ||
| const args = process.argv.slice(2); | ||
| const { showAll, targetDir } = parseArgs(args); | ||
|
|
||
| const files = listDirectory(targetDir, showAll); | ||
| console.log(files.join("\n")); | ||
| } | ||
|
|
||
| main(); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #!/bin/bash | ||
| set -euo pipefail | ||
|
|
||
| show_all=false | ||
|
|
||
| # handle -a | ||
| if [ "${1:-}" = "-a" ]; then | ||
| show_all=true | ||
| shift | ||
| fi | ||
|
|
||
| # directory (default current) | ||
| dir="." | ||
|
|
||
| if [ "${1:-}" != "" ]; then | ||
| dir="$1" | ||
| fi | ||
|
|
||
| #choose pattern | ||
| if [ "$show_all" = true ]; then | ||
| files="$dir"/.* | ||
| else | ||
| files="$dir"/* | ||
| fi | ||
| # loop | ||
| for file in $files | ||
| do | ||
| name="$(basename "$file")" | ||
|
|
||
| if [ "$name" = "." ] || [ "$name" = ".." ]; then | ||
| continue | ||
| fi | ||
| echo "$name" | ||
| done |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| const fs = require("node:fs"); | ||
|
|
||
| function countFile(filePath) { | ||
| const content = fs.readFileSync(filePath, "utf8"); | ||
|
|
||
| const lines = content.split("\n").length - 1; | ||
| const words = content.trim() ? content.trim().split(/\s+/).length : 0; | ||
| const chars = Buffer.byteLength(content, "utf8"); | ||
|
|
||
| return { lines, words, chars }; | ||
| } | ||
|
|
||
| function main() { | ||
| const args = process.argv.slice(2); | ||
|
|
||
| let flag = null; | ||
| let files = []; | ||
|
|
||
| for (const arg of args) { | ||
| if (arg === "-l" || arg === "-w" || arg === "-c") { | ||
| flag = arg; | ||
| } else { | ||
| files.push(arg); | ||
| } | ||
| } | ||
|
|
||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalChars = 0; | ||
|
|
||
| for (const file of files) { | ||
| const { lines, words, chars } = countFile(file); | ||
|
|
||
| totalLines += lines; | ||
| totalWords += words; | ||
| totalChars += chars; | ||
|
|
||
| if (flag === "-l") { | ||
| console.log(`${lines} ${file}`); | ||
| } else if (flag === "-w") { | ||
| console.log(`${words} ${file}`); | ||
| } else if (flag === "-c") { | ||
| console.log(`${chars} ${file}`); | ||
| } else { | ||
| console.log(`${lines} ${words} ${chars} ${file}`); | ||
| } | ||
|
Member
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 works if you specify exactly one flag, but the real |
||
| } | ||
|
|
||
| if (files.length > 1) { | ||
| if (flag === "-l") { | ||
|
Member
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 is quite repetitive with the per-file printing - can you avoid that duplication? |
||
| console.log(`${totalLines} total`); | ||
| } else if (flag === "-w") { | ||
| console.log(`${totalWords} total`); | ||
| } else if (flag === "-c") { | ||
| console.log(`${totalChars} total`); | ||
| } else { | ||
| console.log(`${totalLines} ${totalWords} ${totalChars} total`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| flag="all" | ||
|
|
||
| if [ "${1:-}" = "-l" ]; then | ||
| flag="l" | ||
| shift | ||
| elif [ "${1:-}" = "-w" ]; then | ||
| flag="w" | ||
| shift | ||
| elif [ "${1:-}" = "-c" ]; then | ||
| flag="c" | ||
| shift | ||
| fi | ||
|
|
||
| for file in "$@" | ||
| do | ||
| if [ -d "$file" ]; then | ||
| continue | ||
| fi | ||
|
|
||
| lines=0 | ||
| words=0 | ||
| chars=0 | ||
|
|
||
| while IFS= read -r line | ||
| do | ||
| lines=$((lines + 1)) | ||
| words=$((words + $(echo "$line" | wc -w))) | ||
| chars=$((chars + ${#line} + 1)) | ||
| done <"$file" | ||
|
|
||
| if [ "$flag" = "l" ]; then | ||
| echo "$lines $file" | ||
| elif [ "$flag" = "w" ]; then | ||
| echo "$words $file" | ||
| elif [ "$flag" = "c" ]; then | ||
| echo "$chars $file" | ||
| else | ||
| echo "$lines $words $chars $file" | ||
| fi | ||
| done | ||
|
|
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.
What would happen if someone passed both flags (e.g.
node my-cat.js -b -n /some/file)? What do you think should happen?