Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions implement-shell-tools/cat/cat.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import process from "node:process";
import { promises as fs } from "node:fs";

let args = process.argv.slice(2);
const flags = args.filter((arg) => arg.startsWith("-"));
const filePaths = args.filter((args) => !args.startsWith("-"));
let lineNumber = 1;

for (const filePath of filePaths) {
const content = await fs.readFile(filePath, "utf-8");

if (flags.length == 0) {
process.stdout.write(content);
continue;
}

const lines = content.split("\n");

for (const line of lines) {
if (flags.includes("-b")) {
if (line === "") {
console.log();
} else {
console.log(`${lineNumber}\t${line}`);
lineNumber++;
}
} else if (flags.includes("-n")) {
console.log(`${lineNumber}\t${line}`);
lineNumber++;
}
}
}
22 changes: 22 additions & 0 deletions implement-shell-tools/ls/ls.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import process from "node:process";
import { promises as fs } from "node:fs";

const args = process.argv.slice(2);
const flags = args.filter((arg) => arg.startsWith("-"));
const path = args.filter((arg) => !arg.startsWith("-"));
let targetDir = path[0] || ".";
let files = await fs.readdir(targetDir);

if (flags.includes("-a")) {
files.unshift(".", "..");
} else {
files = files.filter((fileName) => !fileName.startsWith("."));
}

if (flags.includes("-1")) {
for (const file of files) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a more advanced javascript way of printing each item in an array, so you don't have to write a full for loop?

console.log(file);
}
} else {
console.log(files.join(" "));
}
Empty file.
48 changes: 48 additions & 0 deletions implement-shell-tools/wc/wc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import process from "node:process";
import { promises as fs } from "node:fs";

let filePaths = [];
let flag = null;
let numberOfWords;

if (process.argv[2].startsWith("-")) {
flag = process.argv[2];
filePaths = process.argv.slice(3);
} else {
filePaths = process.argv.slice(2);
}
for (const filePath of filePaths) {
const content = await fs.readFile(filePath, "utf-8");

if (flag === "-w") {
console.log(getWordCount(content));
} else if (flag === "-l") {
console.log(getLineCount(content));
} else if (flag === "-c") {
console.log(getByteCount(content), filePaths);
} else {
console.log(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tested, i got output that looks very different to the original wc app. Can you find a way to make it look more similar?

getWordCount(content),
getLineCount(content),
getByteCount(content),
filePaths,
);
}
}

function getWordCount(text) {
const trimmed = text.trim();
if (trimmed.length == 0) {
return 0;
}
return trimmed.split(/\s+/).length;
}

function getLineCount(text) {
if (text.length == 0) return 0;
return text.split("\n").length - 1;
}

function getByteCount(text) {
return Buffer.byteLength(text, "utf-8");
}
Loading