Skip to content

Commit

Permalink
bin: js (execute js functions on input)
Browse files Browse the repository at this point in the history
  • Loading branch information
andys8 committed Jan 28, 2025
1 parent 903d225 commit e8abc00
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions bin/js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node
// Execute JavaScript code from the command line.
// Usage: js "1 + 1"
// Usage: echo 1 | js "x => Number(x) * 2"
// Usage: cat file.txt | js "x => x.split('\n').join(',')"

const args = process.argv.slice(2);

if (args.length === 0) {
console.error("Error: Please provide a value for x as an argument.");
process.exit(1);
}

const x = args[0];
const z = eval(x);

if (typeof z === "function") {
process.stdin.on("data", (data) => {
process.stdout.write(z(data.toString()).toString());
});
} else {
process.stdout.write(z.toString());
}

0 comments on commit e8abc00

Please sign in to comment.