Crates.io | fn |
lib.rs | fn |
version | |
source | src |
created_at | 2025-01-04 18:49:11.708931 |
updated_at | 2025-01-08 21:15:13.895907 |
description | A tool for evaluating a JavaScript function and printing the result. |
homepage | |
repository | https://github.com/callum-oakley/fn |
max_upload_size | |
id | 1504166 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
fn is a tool for evaluating a JavaScript function and printing the result.
Evaluate a JavaScript function and print the result
Usage: fn [OPTIONS] [BODY]
Arguments:
[BODY] The body of the JavaScript function to be evaluated [default: $]
Options:
-p, --parse JSON.parse STDIN before passing it to the function
-s, --stringify JSON.stringify the result before printing it to STDOUT
-h, --help Print help
-V, --version Print version
STDIN is avaialable in BODY as $. Environment variables are available in BODY prefixed by $.
Suppose we have some JSON which contains a bunch of superheros and we want to find the hero with the power of "Immortality":
> curl https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json |
fn -ps '$.members.find(m => m.powers.includes("Immortality"))'
{
"name": "Eternal Flame",
"age": 1000000,
"secretIdentity": "Unknown",
"powers": [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
}
The provided BODY
is evaluated by V8 as part of the expression $ => BODY
. In particular,
this means that BODY
must have the syntax of an Arrow Function body: it can either be a single
expression, or multiple statements enclosed in braces with an explicit return statement.
If STDIN is not a terminal then $
contains the result of reading STDIN as text, or of parsing it
as JSON if the -p
flag is set. If STDIN is a terminal then $
is undefined
.
The result is printed to STDOUT after being cast to a string, or serialized as JSON if the -s
flag is set.
Environment variables are available in BODY
prefixed by $
. e.g. USER
is available as $USER
.
JavaScript is a convenient language with which to process JSON (which stands for "JavaScript Object Notation" after all), but the boilerplate of reading from STDIN, parsing, and writing to STDOUT makes many could-be "one-liners" significantly more involved than they need to be. fn provides a thin wrapper around V8 which handles this boilerplate and makes it more ergonomic to sprinkle a little JavaScript in to a shell script.
fn can be used for many of the same tasks as jq. A given jq command is often a little shorter than the equivalent fn command, but if (like the author) you find yourself often forgetting the syntax of jq, and you already know JavaScript, you might find fn easier to use. To see how fn compares to jq, check out the translated jq tutorial.
First you'll need to install Rust, then
cargo install fn