Giter Site home page Giter Site logo

js-to-json-logic's Introduction

JS to JSONLogic

npm License:MIT GitHub Workflow Status Type Declarations Try It on RunKit

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

js-to-json-logic's People

Contributors

dependabot[bot] avatar krismuniz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

js-to-json-logic's Issues

'substr' not detected

getting error Unexpected token: 'identifier' for the line transformJS('attibutes["time"] substr "234"')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.