Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-to-json-logic

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-to-json-logic

Generate JSON Logic objects using JavaScript syntax.

  • 0.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
891
decreased by-8.62%
Maintainers
1
Weekly downloads
 
Created
Source

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

expressionsupportexamples
Boolean Literalsfulltrue, false
String Literalsfull"banana", "hello world"
Template Literalsfull`hello, ${first_name}`
Numeric Literalsfull1, 2.04, -10292.64, 0b01011010, 0xFF00FF, etc.
Object Expressions / Literalsfull({ a: [1, false, 'string'], b: false, d: 'hello' })
Array Expressionsfull[1, 2, 3]
Spread Operator in Arraysfull[1, 2, ...myArr]
Null Literalsfullnull
Identifiers (variables)fullmyVar, deep.property
Comparison Expressionsfulla > b, a < b, a <= b, a === b, a !== b, etc.
Arithmetic Operatorsfulla + b, a * b, a - b, a / b, a % b
Call ExpressionsfullmyFunction(a, b, c)
Unary Expressionsfull!cond, !!cond, -var, +var
Conditional (Ternary) Operatorfullcondition ? a : b
Regex Literallimited/[^@]+@[^\.]+\..+/gi
If Statementslimitedif (condA) { a } else if (condB) { b } else { c }
Call Expressions with Callbacklimitedmap(arr, x => x + 1)
Arrow Functionslimitedx => 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

Keywords

FAQs

Package last updated on 03 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc