JS to JSONLogic
Transform JavaScript expressions into JSONLogic objects. For Node.js.
NOTICE: This module is still in beta!
Installing
npm install --save js-to-json-logic
Usage
const transformJS = require("js-to-json-logic");
transformJS('temp < 110 && pie.filling === "apple"');
The transformJS
function returns a JavaScript object, which can be stringified as a JSON and look like this:
{
"and": [
{
"<": [
{
"var": "temp"
},
110
]
},
{
"===": [
{
"var": "pie.filling"
},
"apple"
]
}
]
}
How it Works
To parse input code, this module uses @babel/parser
to parse JavaScript code into an Abstract Syntax Tree (AST). The tree is then transformed into a JSONLogic object.
Said object is can then be used with the json-logic-js
module to apply the interpreted rules to any type of data. Learn More
Supported JavaScript Syntax
expression | support | examples |
---|
Boolean Literals | full | true , false |
String Literals | full | "banana" , "hello world" |
Template Literals | full | `hello, ${first_name}` |
Numeric Literals | full | 1 , 2.04 , -10292.64 , 0b01011010 , 0xFF00FF , etc. |
Object Expressions / Literals | full | ({ a: [1, false, 'string'], b: false, d: 'hello' }) |
Array Expressions | full | [1, 2, 3] |
Spread Operator in Arrays | full | [1, 2, ...myArr] |
Null Literals | full | null |
Identifiers (variables) | full | myVar , deep.property |
Comparison Expressions | full | a > b , a < b , a <= b , a === b , a !== b , etc. |
Arithmetic Operators | full | a + b , a * b , a - b , a / b , a % b |
Call Expressions | full | myFunction(a, b, c) |
Unary Expressions | full | !cond , !!cond , -var , +var |
Conditional (Ternary) Operator | full | condition ? a : b |
Regex Literal | limited | /[^@]+@[^\.]+\..+/gi |
If Statements | limited | if (condA) { a } else if (condB) { b } else { c } |
Call Expressions with Callback | limited | map(arr, x => x + 1) |
Arrow Functions | limited | x => x + 1 , (a, b) => a + b |
Notes:
-
Regex Literals: are not supported by the JSON spec. To account for this, they are converted into an array of strings. The first element of the array is the pattern, the second one contains the flags. Example: [ "\d\d\d\d", "gi" ]
-
If Statements: Multi-line consequentials (block statements in if statements) are not supported. Also, implicit return will always apply.
-
Call Expressions with Callback: Only arrow functions are allowed as callbacks in call expressions.
-
Arrow Functions: arrow functions can only be single-line expressions or have a one-line block statement
-
If a specific syntactic JS feature is not specified in the table above, it's likely that it isn't supported. If you have an idea on how to support said feature, feel free to file a GitHub Issue.
In Plans to Support
- Optional Chaining
- Nullish Coalescing
Unsupported Syntax
The following syntactic features are not supported by this module.
- Class Declarations
- Private name expressions
#myPrivateProperty
- Update Expressions (
i++
, i--
, etc.) - Assignment Expressions
- Tagged Template Expressions
- Function Declaration
- While Loops
- For Loops
- Multi-Line Block Statements
new
operator- Destructuring assignment
- Rest operator
- Spread operator (although it works within array expressions)
Contributing
Bug Reports & Feature Requests
Something does not work as expected or perhaps you think this project needs a feature? Please open an issue using GitHub issue tracker.
Make sure that an issue pointing out your specific problem does not exist already. Please be as specific and straightforward as possible.
Pull Requests
Pull Requests (PRs) are welcome! You should follow the same basic stylistic conventions as the original code.
Make sure that a pull request solving your specific problem does not exist already. Your changes must be concise and focus on solving a discrete problem.
License
The MIT License (MIT)
Copyright (c) 2020 Kristian Muñiz