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

expjson

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expjson

Super lightweight, fast, and optimized evaluate-able and compilable expressions in JSON written in TypeScript

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

expjson

Super lightweight, fast, and optimized evaluate-able and compilable expressions in JSON written in TypeScript

npm version npm npm type definitions Build Status Dependency Status GitHub

Get Started

npm install -D expjson

or

yarn add -D expjson

How to Use

compileExpression and running it later is much faster if compiled expression is evaluated multiple times on many execution contexts.

evaluateExpression is faster if the expression is evaluated only once.

functionalities are exactly same for both.

import { compileExpression, evaluateExpression, IfThenElse, In, Not } from 'expjson';


evaluateExpression(
  // expression to evaluate
  [
    IfThenElse, // same as '?:'
    [In, 'admin', '$roles'], // test if "admin" is in "roles" context variable
    [Not, '$postDeleted'], // true if context variable "postDeleted" is false
    '$unauthorized', // context variable "unauthorized"
  ],
  // execution context
  {
    postDeleted: false,
    roles: ['user', 'admin'],
    unauthorized: 'Unauthorized Error',
  }
) === true


evaluateExpression(
  [
    '?:', // same as IfThenElse
    ['In', 'admin', '$roles'], // test if "admin" is in "roles" context variable
    ['!', '$postDeleted'], // true if context variable "postDeleted" is false
    '$unauthorized', // context variable "unauthorized"
  ],
  {
    postDeleted: true,
    roles: ['user', 'admin'],
    unauthorized: 'Unauthorized Error',
  }
) === false


evaluateExpression(
  [
    IfThenElse, // same as '?:'
    [In, 'admin', '$roles'], // test if "admin" is in "roles" context variable
    [Not, '$postDeleted'], // true if context variable "postDeleted" is false
    '$unauthorized', // context variable "unauthorized"
  ],
  {
    postDeleted: false,
    roles: ['user', 'guest'],
    unauthorized: 'Unauthorized Error',
  }
) === 'Unauthorized Error'


const compiled1 = compileExpression([
  IfThenElse, // same as '?:'
  [In, 'admin', '$roles'], // test if "admin" is in "roles" context variable
  [Not, '$postDeleted'], // true if context variable "postDeleted" is false
  '$unauthorized', // context variable "unauthorized"
]);

compiled1({
  postDeleted: false,
  roles: ['user', 'admin'],
  unauthorized: 'Unauthorized Error',
}) === true


const compiled2 = compileExpression([
  '?:', // same as IfThenElse
  ['In', 'admin', '$roles'], // test if "admin" is in "roles" context variable
  ['!', '$postDeleted'], // true if context variable "postDeleted" is false
  '$unauthorized', // context variable "unauthorized"
]);

compiled2({
  postDeleted: true,
  roles: ['user', 'admin'],
  unauthorized: 'Unauthorized Error',
}) === false


const compiled3 = compileExpression([
  IfThenElse, // same as '?:'
  [In, 'admin', '$roles'], // test if "admin" is in "roles" context variable
  [Not, '$postDeleted'], // true if context variable "postDeleted" is false
  '$unauthorized', // context variable "unauthorized"
]);

compiled3({
  postDeleted: false,
  roles: ['user', 'guest'],
  unauthorized: 'Unauthorized Error',
}) === 'Unauthorized Error'

License

MIT License

Keywords

FAQs

Package last updated on 01 Aug 2019

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