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-expression-eval

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-expression-eval

evaluate a json described boolean expression using dynamic functions

1.0.0
Source
npmnpm
Version published
Weekly downloads
420
-2.33%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status Coverage Status

json-expression-eval

A Node.js module that evaluates a json described boolean expression using dynamic functions

Installation

npm install json-expression-eval --save
yarn add json-expression-eval

Usage

  • Please see example dir for extended usage and opinionated usage

API

This module enables you to evaluate boolean expressions which are described using a JSON structure and can utilize user defined 'functions'.
There are 4 types op operators you can use:

  • and - accepts a non empty list of operators
  • or - accepts a non empty list of operators
  • not - accepts another operator
  • <user defined funcs> - accepts any type of argument and evaluated by the user defined functions and given context.

Example expressions, assuming we have the user and maxCount user defined functions in place can be:

{  
   "or":[  
      {  
         "not":{  
            "user":"a@b.com"
         }
      },
      {  
         "maxCount":1
      }
   ]
}

Javascript

const evaluator = require('json-expression-eval');
const functionsTable = {
	user: (user, context) => {
        return context.userId === user;
    },
	maxCount: (maxCount, context) => {
        return context.times < maxCount;
    }
};
const result = evaluator.evaluate({or:[{user: 'a@b.com'},{not: {and: [{maxCount: 1},{user: 'a2@b.com'}]}}]},{userId:'a@b.com', times: 1},functionsTable);
Output should be 'true'

TypeScript

import { evaluate } from 'json-expression-eval';
const functionsTable : FunctionsTable = {
	user: (user :string , context : { userId: string }): boolean => {
        return context.userId === user;
    },
	maxCount: (maxCount: number, context: { times: number }): boolean => {
        return context.times < maxCount;
    }
};
const result = evaluate({or:[{user: 'a@b.com'},{not: {and: [{maxCount: 5},{user: 'a2@b.com'}]}}]},{userId:'a2@b.com', times: 1},functionsTable);
Output should be 'false'

Test

npm run test

Keywords

evaluate

FAQs

Package last updated on 09 Jun 2018

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