@lukecsamuel/expresso v1.2.2
Expresso
A compiler (transpiler) to convert human-readable expressions into JsonLogic.
Expresso Language
The "Expresso" language provides a more human-readable syntax for basic expressions than JsonLogic. It supports a subset of operations provided by JsonLogic.
Literals
The following literals are supported:
- Numbers: in decimal e.g.
1.5
or hex e.g.0xFFF
- Strings: using single quotes e.g.
'hello'
or double e.g."hello"
- Boolean:
true
orfalse
- Null:
null
Variables
Any non-reserved word compiles into a var
rule:
foo
=== becomes ===
{ "var": "foo" }
Logical Operators
Logical operators are implemented using the keywords and
, or
, and not
:
not foo and bar or baz
=== becomes ===
{
"or": [
{
"and": [
{
"!": [
{ "var": "foo" }
]
},
{ "var": "bar" },
]
},
{ "var": "baz" }
]
}
Arithmetic Operators
Basic arithmetic operators are supported using their standard symbols: +
, -
, *
, and /
. Expressions can be grouped using parenthesis to modify their order of evaluation:
1 * (2 + 3)
=== becomes ===
{
"*": [
1,
{
"+": [
2,
3
]
}
]
}
Lists
A list of expressions can be defined using square brackets:
[1 + 2, foo]
=== becomes ===
[
{
"+": [1, 2]
},
{
"var": "foo"
}
]
Compilation
Install @lukecsamuel/expresso
:
npm i @lukecsamuel/expresso
yarn add @lukecsamuel/expresso
Import expresso
and call it with the string expression that should be transpiled to JsonLogic:
import { expresso } from '@lukecsamuel/expresso';
const { jsonLogic } = expresso('foo = "bar"');
console.log(jsonLogic); // "{ '===': [{ var: 'foo' }, 'bar']}"
Error Treatment
You can pass options to expresso
to indicate how it handles errors:
expresso('foo = "bar"', {
logErrors: true, // Log errors to the console
throwParseErrors: true, // Throw only parsing errors, which occur when the input is invalid
throwAllErrors: true, // Throw any error encountered while running the compiler
});
Post-Compilation Transforms
Expresso supports post-compilation transform functions to run on the generated output. The can be specified in an array in the options object.
Out of the box, expresso provides the treatIdentifiersAsArrays
transform function that wraps identifiers in some
rules if they are part of a boolean rule. This transform is experimental, so be careful when using it.
import { treatIdentifiersAsArrays } from '@lukecsamuel/expresso/post';
expresso('foo in [1, 2, 3]', {
postCompile: [treatIdentifiersAsArrays],
});