Skip to content

Formula catalog

These composite formulas ship with Jx and appear in Studio's formula palette. Inserting one copies its pure $expression body into your document — there is no runtime dependency on this catalog. The blessed operator set the bodies draw from is in the operator reference .

average

Arithmetic mean of an array of numbers; 0 for an empty array.

Parameter Type Description
values number[]
{
  "operator": "?:",
  "target": {
    "$ref": "$args/values/length"
  },
  "value": {
    "operator": "/",
    "target": {
      "operator": "reduce",
      "target": {
        "$ref": "$args/values"
      },
      "initial": 0,
      "value": {
        "operator": "+",
        "target": {
          "$ref": "$reduce/acc"
        },
        "value": {
          "$ref": "$map/item"
        }
      }
    },
    "value": {
      "$ref": "$args/values/length"
    }
  },
  "initial": 0
}

capitalize

Uppercase the first character of a string, leaving the rest unchanged (the CSS text-transform: capitalize shape for a single word).

Parameter Type Description
text string
{
  "operator": "+",
  "target": {
    "operator": "toUpperCase",
    "target": {
      "operator": "charAt",
      "target": {
        "$ref": "$args/text"
      },
      "value": 0
    }
  },
  "value": {
    "operator": "slice",
    "target": {
      "$ref": "$args/text"
    },
    "value": 1
  }
}

clamp

Bound a number to the [min, max] range (the TC39 Math.clamp shape): returns min when below, max when above, the value itself otherwise.

Parameter Type Description
value number
min number
max number
{
  "operator": "call",
  "target": {
    "$ref": "window#/Math/min"
  },
  "value": [
    {
      "operator": "call",
      "target": {
        "$ref": "window#/Math/max"
      },
      "value": [
        {
          "$ref": "$args/value"
        },
        {
          "$ref": "$args/min"
        }
      ]
    },
    {
      "$ref": "$args/max"
    }
  ]
}

compact

A copy of an array with the falsy elements removed (Array.prototype.filter on truthiness).

Parameter Type Description
values unknown[]
{
  "operator": "filter",
  "target": {
    "$ref": "$args/values"
  },
  "value": {
    "operator": "!",
    "target": {
      "operator": "!",
      "target": {
        "$ref": "$map/item"
      }
    }
  }
}

count

The length of an array or string, or 0 when the value is missing.

Parameter Type Description
values unknown[] string
{
  "operator": "??",
  "target": {
    "$ref": "$args/values/length"
  },
  "value": 0
}

first

The first element of an array (index 0), or null when empty.

Parameter Type Description
values unknown[]
{
  "operator": "??",
  "target": {
    "$ref": "$args/values/0"
  },
  "value": null
}

initials

The uppercase first letters of each whitespace-separated word, joined (e.g. "ada lovelace" → "AL").

Parameter Type Description
name string
{
  "operator": "join",
  "target": {
    "operator": "map",
    "target": {
      "operator": "split",
      "target": {
        "$ref": "$args/name"
      },
      "value": " "
    },
    "value": {
      "operator": "toUpperCase",
      "target": {
        "operator": "charAt",
        "target": {
          "$ref": "$map/item"
        },
        "value": 0
      }
    }
  },
  "value": ""
}

isEmpty

True when an array or string has no elements (length 0 — a missing value counts as empty).

Parameter Type Description
value unknown[] string
{
  "operator": "===",
  "target": {
    "operator": "??",
    "target": {
      "$ref": "$args/value/length"
    },
    "value": 0
  },
  "value": 0
}

last

The last element of an array (Array.prototype.at(-1)), or undefined when empty.

Parameter Type Description
values unknown[]
{
  "operator": "at",
  "target": {
    "$ref": "$args/values"
  },
  "value": -1
}

max

The largest number in an array (Math.max folded over the values); undefined for an empty array.

Parameter Type Description
values number[]
{
  "operator": "reduce",
  "target": {
    "$ref": "$args/values"
  },
  "initial": {
    "$ref": "$args/values/0"
  },
  "value": {
    "operator": "call",
    "target": {
      "$ref": "window#/Math/max"
    },
    "value": [
      {
        "$ref": "$reduce/acc"
      },
      {
        "$ref": "$map/item"
      }
    ]
  }
}

min

The smallest number in an array (Math.min folded over the values); undefined for an empty array.

Parameter Type Description
values number[]
{
  "operator": "reduce",
  "target": {
    "$ref": "$args/values"
  },
  "initial": {
    "$ref": "$args/values/0"
  },
  "value": {
    "operator": "call",
    "target": {
      "$ref": "window#/Math/min"
    },
    "value": [
      {
        "$ref": "$reduce/acc"
      },
      {
        "$ref": "$map/item"
      }
    ]
  }
}

percent

part / whole as a percentage (0 when the whole is 0 or missing).

Parameter Type Description
part number
whole number
{
  "operator": "?:",
  "target": {
    "$ref": "$args/whole"
  },
  "value": {
    "operator": "*",
    "target": {
      "operator": "/",
      "target": {
        "$ref": "$args/part"
      },
      "value": {
        "$ref": "$args/whole"
      }
    },
    "value": 100
  },
  "initial": 0
}

roundTo

Round a number to the given decimal places (Math.round against a Math.pow(10, digits) scale).

Parameter Type Description
value number
digits number
{
  "operator": "/",
  "target": {
    "operator": "call",
    "target": {
      "$ref": "window#/Math/round"
    },
    "value": [
      {
        "operator": "*",
        "target": {
          "$ref": "$args/value"
        },
        "value": {
          "operator": "call",
          "target": {
            "$ref": "window#/Math/pow"
          },
          "value": [
            10,
            {
              "$ref": "$args/digits"
            }
          ]
        }
      }
    ]
  },
  "value": {
    "operator": "call",
    "target": {
      "$ref": "window#/Math/pow"
    },
    "value": [
      10,
      {
        "$ref": "$args/digits"
      }
    ]
  }
}

sum

Add every number in an array (Array.prototype.reduce with +, seeded at 0).

Parameter Type Description
values number[]
{
  "operator": "reduce",
  "target": {
    "$ref": "$args/values"
  },
  "initial": 0,
  "value": {
    "operator": "+",
    "target": {
      "$ref": "$reduce/acc"
    },
    "value": {
      "$ref": "$map/item"
    }
  }
}

truncate

Shorten a string to at most the given length, appending a horizontal ellipsis when cut.

Parameter Type Description
text string
length number
{
  "operator": "?:",
  "target": {
    "operator": ">",
    "target": {
      "$ref": "$args/text/length"
    },
    "value": {
      "$ref": "$args/length"
    }
  },
  "value": {
    "operator": "+",
    "target": {
      "operator": "slice",
      "target": {
        "$ref": "$args/text"
      },
      "value": [
        0,
        {
          "$ref": "$args/length"
        }
      ]
    },
    "value": "…"
  },
  "initial": {
    "$ref": "$args/text"
  }
}