You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

json-logic-engine

Package Overview
Dependencies
Maintainers
1
Versions
104
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-logic-engine

Construct complex rules with JSON & process them.

0.0.25
Source
npmnpm
Version published
Weekly downloads
12K
5.44%
Maintainers
1
Weekly downloads
 
Created
Source

JSON Logic Engine

JavaScript Style Guide

npm version Coverage Status Build Status

This library was developed to be (for the most part) a drop-in replacement for the popular npm module json-logic-js, which at the time of writing has an vulnerability with prototype pollution.

The intention of the library is to keep the functionality very similar to the original, while adding a few new notable features.

The library has an async version of the engine, so if an operation needed to be added that needed to process asynchronously, the library is capable of handling it.

Examples:

The premise is the logic engine traverses the document you pass in, and each "object" is interpreted as an instruction for the engine to run.

logic.run({
    '+': [1,2,3,4,5]
}) // 15

If you wanted to start factoring variables, you can pass a data object into it, and reference them using the "var" instruction:

logic.run({
    '+': [11, { var: 'a' }]
}, {
    'a': 17
}) // 28

The engine will also allow you to reference variables that are several layers deep:

logic.run({
    '+': [{ var: 'a.b.c' }, 5]
}, {
    a: { b: { c: 7 } }
}) // 12

Let's explore some slightly more complex logic:

logic.run({
    'reduce': [{ var: 'x' }, { '+': [{ var: 'current' }, { var: 'accumulator' }] }, 0]
}, {
    'x': [1,2,3,4,5]
}) // 15

In this example, we run the reduce operation on a variable called "x", and we set up instructions to add the "current" value to the "accumulator", which we have set to 0.

Similarly, you can also do map operations:

logic.run({
    'map': [[1,2,3,4,5], { '+': [{ var: '' }, 1] }]
}) // [2,3,4,5,6]

If var is left as an empty string, it will assume you're referring to the whole variable that is accessible at the current layer it is looking at.

Example of a map accessing variables of the objects in the array:

logic.run({
    'map': [{var : 'x'}, { '+': [{ var: 'a' }, 1] }]
},
{
    'x': [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }]
}) // [2,3,4,5]

You can easily nest different operations in each other, like so:

logic.run({
    max: [200, {
        '*': [12, {var: 'a' }]
    }]
}, {
    a: 16
}) // 200

The engine also supports Handlebars-esque style traversal of data when you use the iterative control structures.

For example:

logic.run({
    'map': [{var : 'x'}, { '+': [{ var: 'a' }, { var: '../../adder'}] }]
},
{
    'x': [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }],
    'adder':  7
}) // [8, 9, 10, 11]

FAQs

Package last updated on 09 Apr 2021

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.