npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-to-json-logic

v0.1.3

Published

Generate JSON Logic objects using JavaScript syntax.

Downloads

3,916

Readme

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