This is a basic project to convert plain javaScript to json-logic
notation.
Installation:
npm install js2jl
or with yarn
yarn add js2jl
Usage:
import js2jl from 'js2jl'
const infix = "A + B - 2"
const logic = js2jl(infix)
// logic contains:
// {
// "-": [
// {
// "+": [
// {
// "var": "A"
// },
// {
// "var": "B"
// }
// ]
// },
// 2
// ]
// }
Currently supported operators (ordered by precedency):
*
,/
,%
;+
,-
,cat
,in
,substr
;===
,!==
,==
,!=
,<
,>
,<=
,>=
,or
,and
;?
followed by:
,filter
,map
,reduce
followed byinitial
,method
followed byarguments
.
Only if
, reduce
and method
are currently supporting three arguments; the rest uses only two.
Arrays, objects, and strings are being parsed (but only strings that don't contain any spaces for now); anything else is treated as a variable.
Important:
- Operators and operands must be separated by a space.
- to make
{"var": ""}
use two spaces one after another.
Here's a sandbox with this project running