-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bin: js (execute js functions on input)
- Loading branch information
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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()); | ||
} |