Expressions

Learn about the expression syntax available in templates.

Templates support dynamic values using the ${expression} syntax. Expressions use a subset of JavaScript and can appear in both keys and values throughout a template.

You can find the full list of available variables and functions in the variables and functions references.

Resolution

When a value consists entirely of a single expression, the result preserves the original type. If the expression evaluates to a number, the result is a number, not a string. This lets you pass non-string values like numbers, booleans, objects, and arrays through expressions.

If the value contains anything outside the expression, even a leading space, it is treated as string interpolation and the result is always a string.

For example, consider these two templates where the outer passes a value to the inner:

inner.json5
{  "$schema": "https://schema.croct.com/json/v1/template.json",  "title": "Inner",  "description": "Expects a number option.",  "options": {    "count": {      "type": "number",      "description": "The item count."    }  },  "actions": [    {      "name": "print",      "message": "You have ${options.count} items."    }  ]}
template.json5
{  "$schema": "https://schema.croct.com/json/v1/template.json",  "title": "Outer",  "description": "Passes a number to another template.",  "actions": [    {      "name": "import",      "template": "./inner.json5",      "options": {        // Resolves to the number 42, not the string "42"        "count": "${40 + 2}"      }    }  ]}

Running this template prints You have 42 items. because "${40 + 2}" resolves to the number 42. Writing "Total: ${40 + 2}" instead would produce the string "Total: 42", and the CLI would report the error:

Expected value of type number for option count, but got string.

Operators

Expressions support the following operators:

CategoryOperators
Arithmetic+, -, *, /, %, **
Comparison==, !=, ===, !==, <, >, <=, >=
Logical&&, ||, !
Nullish coalescing??
Conditionalcondition ? then : else

Literals

The following literal types are supported:

TypeSyntax
Strings'hello', "world"
Numbers42, 3.14
Booleanstrue, false
Nullnull
Arrays[1, 2, 3]
Objects{key: 'value'}
Regex/pattern/flags

Property access

You can use dot or bracket notation to access properties:

"source": "${project.path.source}","name": "${options['name']}"

Built-in API

Expressions support a subset of JavaScript methods and properties. See below for the full list available for each value type.

Strings

NameDescription
lengthThe number of characters in the string.
sliceExtracts a section of the string.
indexOfReturns the index of the first occurrence of a value.
matchMatches the string against a regular expression.
matchAllReturns all matches of a regular expression.
replaceReplaces the first match of a pattern.
replaceAllReplaces all matches of a pattern.
includesChecks whether the string contains a value.
startsWithChecks whether the string starts with a value.
endsWithChecks whether the string ends with a value.
toLowerCaseConverts the string to lowercase.
toUpperCaseConverts the string to uppercase.
repeatRepeats the string a given number of times.
splitSplits the string into an array of substrings.

Arrays

NameDescription
lengthThe number of elements in the array.
sliceReturns a shallow copy of a portion of the array.
joinJoins all elements into a string.
includesChecks whether the array contains a value.
indexOfReturns the index of the first occurrence of a value.

Regular expressions

NameDescription
sourceThe pattern text of the regular expression.
testTests whether the pattern matches a string.