expjson
Super lightweight, fast, and optimized evaluate-able and compilable expressions in JSON written in TypeScript
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(
[
IfThenElse,
[In, 'admin', '$roles'],
[Not, '$postDeleted'],
'$unauthorized',
],
{
postDeleted: false,
roles: ['user', 'admin'],
unauthorized: 'Unauthorized Error',
}
) === true
evaluateExpression(
[
'?:',
['In', 'admin', '$roles'],
['!', '$postDeleted'],
'$unauthorized',
],
{
postDeleted: true,
roles: ['user', 'admin'],
unauthorized: 'Unauthorized Error',
}
) === false
evaluateExpression(
[
IfThenElse,
[In, 'admin', '$roles'],
[Not, '$postDeleted'],
'$unauthorized',
],
{
postDeleted: false,
roles: ['user', 'guest'],
unauthorized: 'Unauthorized Error',
}
) === 'Unauthorized Error'
const compiled1 = compileExpression([
IfThenElse,
[In, 'admin', '$roles'],
[Not, '$postDeleted'],
'$unauthorized',
]);
compiled1({
postDeleted: false,
roles: ['user', 'admin'],
unauthorized: 'Unauthorized Error',
}) === true
const compiled2 = compileExpression([
'?:',
['In', 'admin', '$roles'],
['!', '$postDeleted'],
'$unauthorized',
]);
compiled2({
postDeleted: true,
roles: ['user', 'admin'],
unauthorized: 'Unauthorized Error',
}) === false
const compiled3 = compileExpression([
IfThenElse,
[In, 'admin', '$roles'],
[Not, '$postDeleted'],
'$unauthorized',
]);
compiled3({
postDeleted: false,
roles: ['user', 'guest'],
unauthorized: 'Unauthorized Error',
}) === 'Unauthorized Error'
License
MIT License