0.0.6 • Published 5 years ago

expjson v0.0.6

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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,
  // operators
  Add,
  And,
  Divide,
  Equal,
  GreaterThan,
  GreaterThanOrEqual,
  IfThenElse,
  In,
  LessThan,
  LessThanOrEqual,
  Modulo,
  Multiply,
  Not,
  NotEqual,
  NotIn,
  Or,
  Subtract,
  Var,
} from 'expjson';

// evaluateExpression: evaluate without compile
expect(
  evaluateExpression(
    [
      IfThenElse, // same as '?:'
      [In, 'admin', [Var, 'roles']], // test if "admin" is in "roles" context variable
      [Not, [Var, 'postDeleted']], // true if context variable "postDeleted" is false
      [Var, 'unauthorized'], // context variable "unauthorized"
    ],
    {
      postDeleted: false,
      roles: ['user', 'admin'],
      unauthorized: 'Unauthorized Error',
    }
  )
).toBe(true);

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

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

// compileExpression: compile then evaluate
const compiled1 = compileExpression([
  IfThenElse, // same as '?:'
  [In, 'admin', [Var, 'roles']], // test if "admin" is in "roles" context variable
  [Not, [Var, 'postDeleted']], // true if context variable "postDeleted" is false
  [Var, 'unauthorized'], // context variable "unauthorized"
]);
expect(
  compiled1({
    postDeleted: false,
    roles: ['user', 'admin'],
    unauthorized: 'Unauthorized Error',
  })
).toBe(true);

const compiled2 = compileExpression([
  '?:', // same as IfThenElse
  ['In', 'admin', [Var, 'roles']], // test if "admin" is in "roles" context variable
  ['!', [Var, 'postDeleted']], // true if context variable "postDeleted" is false
  [Var, 'unauthorized'], // context variable "unauthorized"
]);
expect(
  compiled2({
    postDeleted: true,
    roles: ['user', 'admin'],
    unauthorized: 'Unauthorized Error',
  })
).toBe(false);

const compiled3 = compileExpression([
  IfThenElse, // same as '?:'
  [In, 'admin', [Var, 'roles']], // test if "admin" is in "roles" context variable
  [Not, [Var, 'postDeleted']], // true if context variable "postDeleted" is false
  [Var, 'unauthorized'], // context variable "unauthorized"
]);
expect(
  compiled3({
    postDeleted: false,
    roles: ['user', 'guest'],
    unauthorized: 'Unauthorized Error',
  })
).toBe('Unauthorized Error');

License

MIT License

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago