Skip to content

How to use multiline arguments with fx?

Example

Pass a function as an argument.

bash
fx data.json 'x => {
  console.log(x);
  return x.value;
}'

How does it work?

Fx wraps each argument into a function call.

For example, this one:

bash
fx data.json 'x.value'

Will be transformed into JavaScript like:

js
function (x) {
  return x.value;
}

So if you write a multiline argument, the resulting function will be not valid.

bash
fx data.json 'console.log(x);
              x.value'
js
function (x) {
  return console.log(x);
  x.value
}

And the result will be undefined.

Wrap your multiline argument into an arrow function:

bash
fx data.json 'x => {
  console.log(x);
  return x.value;
}'

So the resulting function will be valid JavaScript.

js
function (x) {
  return x => {
    console.log(x);
    return x.value;
  }
}

Note

Fx checks if the returned value is a function and applies it to the JSON data. This is done to be able to run arguments like fx keys.

js
if (typeof result === 'function') return result(json)
return result