Comparing version 0.0.3 to 0.0.4
import { Expression } from './ts'; | ||
export declare const variablePattern: RegExp; | ||
export declare const isOperator: (op: unknown) => op is "==" | "!=" | ">" | ">=" | "<" | "<=" | "+" | "-" | "*" | "/" | "%" | "!" | "&&" | "||" | "?:" | "In" | "!In"; | ||
export declare const isOperator: (op: unknown) => op is "==" | "!=" | ">" | ">=" | "<" | "<=" | "+" | "-" | "*" | "/" | "%" | "!" | "&&" | "||" | "?:" | "In" | "!In" | "$"; | ||
export declare const isExpression: (exp: unknown) => exp is Expression; | ||
//# sourceMappingURL=common.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var operatorMap_1 = require("./operatorMap"); | ||
exports.variablePattern = /^\$[a-z][\w-]*$/i; | ||
exports.isOperator = function (op) { | ||
@@ -6,0 +5,0 @@ return typeof op === 'string' && operatorMap_1.OperatorToName.hasOwnProperty(op); |
@@ -6,16 +6,3 @@ "use strict"; | ||
exports.compileOperand = function (operand) { | ||
if (common_1.isExpression(operand)) { | ||
return exports.compileExpression(operand); | ||
} | ||
if (typeof operand === 'string') { | ||
if (common_1.variablePattern.test(operand)) { | ||
var name_1 = operand.substring(1); | ||
return function (context) { return context[name_1]; }; | ||
} | ||
if (operand.lastIndexOf('\\$', 0) === 0) { | ||
var unescaped_1 = operand.substring(1); | ||
return function () { return unescaped_1; }; | ||
} | ||
} | ||
return function () { return operand; }; | ||
return common_1.isExpression(operand) ? exports.compileExpression(operand) : function () { return operand; }; | ||
}; | ||
@@ -193,4 +180,4 @@ exports.compileExpression = function (exp) { | ||
var exps = exp[2]; | ||
if (typeof exps === 'string') { | ||
var getValues_1 = exports.compileOperand(exps); | ||
if (common_1.isExpression(exps)) { | ||
var getValues_1 = exports.compileExpression(exps); | ||
return function (context) { | ||
@@ -226,4 +213,8 @@ var v = getV_1(context); | ||
} | ||
case operator_1.Var: { | ||
var name_1 = exp[1]; | ||
return function (context) { return context[name_1]; }; | ||
} | ||
} | ||
}; | ||
//# sourceMappingURL=compileExpression.js.map |
@@ -6,14 +6,3 @@ "use strict"; | ||
exports.evaluateOperand = function (operand, context) { | ||
if (common_1.isExpression(operand)) { | ||
return exports.evaluateExpression(operand, context); | ||
} | ||
if (typeof operand === 'string') { | ||
if (common_1.variablePattern.test(operand)) { | ||
return context[operand.substring(1)]; | ||
} | ||
if (operand.lastIndexOf('\\$', 0) === 0) { | ||
return operand.substring(1); | ||
} | ||
} | ||
return operand; | ||
return common_1.isExpression(operand) ? exports.evaluateExpression(operand, context) : operand; | ||
}; | ||
@@ -137,4 +126,4 @@ exports.evaluateExpression = function (exp, context) { | ||
var exps = exp[2]; | ||
if (typeof exps === 'string') { | ||
var vs = exports.evaluateOperand(exps, context); | ||
if (common_1.isExpression(exps)) { | ||
var vs = exports.evaluateExpression(exps, context); | ||
if (Array.isArray(vs)) { | ||
@@ -160,4 +149,4 @@ for (var i = 0, len = vs.length; i < len; i += 1) { | ||
var exps = exp[2]; | ||
if (typeof exps === 'string') { | ||
var vs = exports.evaluateOperand(exps, context); | ||
if (common_1.isExpression(exps)) { | ||
var vs = exports.evaluateExpression(exps, context); | ||
if (Array.isArray(vs)) { | ||
@@ -180,4 +169,7 @@ for (var i = 0, len = vs.length; i < len; i += 1) { | ||
} | ||
case operator_1.Var: { | ||
return context[exp[1]]; | ||
} | ||
} | ||
}; | ||
//# sourceMappingURL=evaluateExpression.js.map |
@@ -1,7 +0,7 @@ | ||
export { isExpression, isOperator, variablePattern } from './common'; | ||
export { isExpression, isOperator } from './common'; | ||
export { EvaluateExpression, compileExpression, compileOperand, } from './compileExpression'; | ||
export { evaluateExpression, evaluateOperand } from './evaluateExpression'; | ||
export { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, } from './operator'; | ||
export { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, Var, } from './operator'; | ||
export { Operator, OperatorName, OperatorToName } from './operatorMap'; | ||
export { ExecutionContext, Expression, Expression1, Expression2, Expression3, Value, Value2, Value3, Value4, } from './ts'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,3 +6,2 @@ "use strict"; | ||
exports.isOperator = common_1.isOperator; | ||
exports.variablePattern = common_1.variablePattern; | ||
var compileExpression_1 = require("./compileExpression"); | ||
@@ -32,4 +31,5 @@ exports.compileExpression = compileExpression_1.compileExpression; | ||
exports.Subtract = operator_1.Subtract; | ||
exports.Var = operator_1.Var; | ||
var operatorMap_1 = require("./operatorMap"); | ||
exports.OperatorToName = operatorMap_1.OperatorToName; | ||
//# sourceMappingURL=index.js.map |
@@ -35,2 +35,4 @@ export declare type Equal = '=='; | ||
export declare const NotIn: NotIn; | ||
export declare type Var = '$'; | ||
export declare const Var: Var; | ||
//# sourceMappingURL=operator.d.ts.map |
@@ -20,2 +20,3 @@ "use strict"; | ||
exports.NotIn = '!In'; | ||
exports.Var = '$'; | ||
//# sourceMappingURL=operator.js.map |
@@ -22,3 +22,4 @@ import * as Operators from './operator'; | ||
'!In': 'NotIn'; | ||
$: 'Var'; | ||
}; | ||
//# sourceMappingURL=operatorMap.d.ts.map |
@@ -1,10 +0,10 @@ | ||
import { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract } from './operator'; | ||
import { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, Var } from './operator'; | ||
export declare type Value = null | boolean | number | string | Array<null | boolean | number | string>; | ||
export declare type Expression1 = [Equal, Value, Value] | [NotEqual, Value, Value] | [GreaterThan, Value, Value] | [GreaterThanOrEqual, Value, Value] | [LessThan, Value, Value] | [LessThanOrEqual, Value, Value] | [Add, Value, Value] | [Subtract, Value, Value] | [Multiply, Value, Value] | [Divide, Value, Value] | [Modulo, Value, Value] | [Not, Value] | [And, ...Value[]] | [Or, ...Value[]] | [IfThenElse, Value, Value, Value] | [In, Value, string | Value[]] | [NotIn, Value, string | Value[]]; | ||
export declare type Expression1 = [Equal, Value, Value] | [NotEqual, Value, Value] | [GreaterThan, Value, Value] | [GreaterThanOrEqual, Value, Value] | [LessThan, Value, Value] | [LessThanOrEqual, Value, Value] | [Add, Value, Value] | [Subtract, Value, Value] | [Multiply, Value, Value] | [Divide, Value, Value] | [Modulo, Value, Value] | [Not, Value] | [And, ...Value[]] | [Or, ...Value[]] | [IfThenElse, Value, Value, Value] | [In, Value, Value[]] | [NotIn, Value, Value[]] | [Var, string]; | ||
export declare type Value2 = Value | Expression1; | ||
export declare type Expression2 = [Equal, Value2, Value2] | [NotEqual, Value2, Value2] | [GreaterThan, Value2, Value2] | [GreaterThanOrEqual, Value2, Value2] | [LessThan, Value2, Value2] | [LessThanOrEqual, Value2, Value2] | [Add, Value2, Value2] | [Subtract, Value2, Value2] | [Multiply, Value2, Value2] | [Divide, Value2, Value2] | [Modulo, Value2, Value2] | [Not, Value2] | [And, ...Value2[]] | [Or, ...Value2[]] | [IfThenElse, Value2, Value2, Value2] | [In, Value2, string | Value2[]] | [NotIn, Value2, string | Value2[]]; | ||
export declare type Expression2 = [Equal, Value2, Value2] | [NotEqual, Value2, Value2] | [GreaterThan, Value2, Value2] | [GreaterThanOrEqual, Value2, Value2] | [LessThan, Value2, Value2] | [LessThanOrEqual, Value2, Value2] | [Add, Value2, Value2] | [Subtract, Value2, Value2] | [Multiply, Value2, Value2] | [Divide, Value2, Value2] | [Modulo, Value2, Value2] | [Not, Value2] | [And, ...Value2[]] | [Or, ...Value2[]] | [IfThenElse, Value2, Value2, Value2] | [In, Value2, Value2[]] | [NotIn, Value2, Value2[]] | [Var, string]; | ||
export declare type Value3 = Value | Expression1 | Value2 | Expression2; | ||
export declare type Expression3 = [Equal, Value3, Value3] | [NotEqual, Value3, Value3] | [GreaterThan, Value3, Value3] | [GreaterThanOrEqual, Value3, Value3] | [LessThan, Value3, Value3] | [LessThanOrEqual, Value3, Value3] | [Add, Value3, Value3] | [Subtract, Value3, Value3] | [Multiply, Value3, Value3] | [Divide, Value3, Value3] | [Modulo, Value3, Value3] | [Not, Value3] | [And, ...Value3[]] | [Or, ...Value3[]] | [IfThenElse, Value3, Value3, Value3] | [In, Value3, string | Value3[]] | [NotIn, Value3, string | Value3[]]; | ||
export declare type Expression3 = [Equal, Value3, Value3] | [NotEqual, Value3, Value3] | [GreaterThan, Value3, Value3] | [GreaterThanOrEqual, Value3, Value3] | [LessThan, Value3, Value3] | [LessThanOrEqual, Value3, Value3] | [Add, Value3, Value3] | [Subtract, Value3, Value3] | [Multiply, Value3, Value3] | [Divide, Value3, Value3] | [Modulo, Value3, Value3] | [Not, Value3] | [And, ...Value3[]] | [Or, ...Value3[]] | [IfThenElse, Value3, Value3, Value3] | [In, Value3, Value3[]] | [NotIn, Value3, Value3[]] | [Var, string]; | ||
export declare type Value4 = Value | Expression1 | Value2 | Expression2 | Value3 | Expression3; | ||
export declare type Expression = [Equal, Value4, Value4] | [NotEqual, Value4, Value4] | [GreaterThan, Value4, Value4] | [GreaterThanOrEqual, Value4, Value4] | [LessThan, Value4, Value4] | [LessThanOrEqual, Value4, Value4] | [Add, Value4, Value4] | [Subtract, Value4, Value4] | [Multiply, Value4, Value4] | [Divide, Value4, Value4] | [Modulo, Value4, Value4] | [Not, Value4] | [And, ...Value4[]] | [Or, ...Value4[]] | [IfThenElse, Value4, Value4, Value4] | [In, Value4, string | Value4[]] | [NotIn, Value4, string | Value4[]]; | ||
export declare type Expression = [Equal, Value4, Value4] | [NotEqual, Value4, Value4] | [GreaterThan, Value4, Value4] | [GreaterThanOrEqual, Value4, Value4] | [LessThan, Value4, Value4] | [LessThanOrEqual, Value4, Value4] | [Add, Value4, Value4] | [Subtract, Value4, Value4] | [Multiply, Value4, Value4] | [Divide, Value4, Value4] | [Modulo, Value4, Value4] | [Not, Value4] | [And, ...Value4[]] | [Or, ...Value4[]] | [IfThenElse, Value4, Value4, Value4] | [In, Value4, Value4[]] | [NotIn, Value4, Value4[]] | [Var, string]; | ||
export interface ExecutionContext { | ||
@@ -11,0 +11,0 @@ [key: string]: Value; |
import { Expression } from './ts'; | ||
export declare const variablePattern: RegExp; | ||
export declare const isOperator: (op: unknown) => op is "==" | "!=" | ">" | ">=" | "<" | "<=" | "+" | "-" | "*" | "/" | "%" | "!" | "&&" | "||" | "?:" | "In" | "!In"; | ||
export declare const isOperator: (op: unknown) => op is "==" | "!=" | ">" | ">=" | "<" | "<=" | "+" | "-" | "*" | "/" | "%" | "!" | "&&" | "||" | "?:" | "In" | "!In" | "$"; | ||
export declare const isExpression: (exp: unknown) => exp is Expression; | ||
//# sourceMappingURL=common.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const operatorMap_1 = require("./operatorMap"); | ||
exports.variablePattern = /^\$[a-z][\w-]*$/i; | ||
exports.isOperator = (op) => typeof op === 'string' && operatorMap_1.OperatorToName.hasOwnProperty(op); | ||
exports.isExpression = (exp) => Array.isArray(exp) && exports.isOperator(exp[0]); | ||
//# sourceMappingURL=common.js.map |
@@ -5,18 +5,3 @@ "use strict"; | ||
const operator_1 = require("./operator"); | ||
exports.compileOperand = (operand) => { | ||
if (common_1.isExpression(operand)) { | ||
return exports.compileExpression(operand); | ||
} | ||
if (typeof operand === 'string') { | ||
if (common_1.variablePattern.test(operand)) { | ||
const name = operand.substring(1); | ||
return (context) => context[name]; | ||
} | ||
if (operand.lastIndexOf('\\$', 0) === 0) { | ||
const unescaped = operand.substring(1); | ||
return () => unescaped; | ||
} | ||
} | ||
return () => operand; | ||
}; | ||
exports.compileOperand = (operand) => common_1.isExpression(operand) ? exports.compileExpression(operand) : () => operand; | ||
exports.compileExpression = (exp) => { | ||
@@ -187,4 +172,4 @@ switch (exp[0]) { | ||
const exps = exp[2]; | ||
if (typeof exps === 'string') { | ||
const getValues = exports.compileOperand(exps); | ||
if (common_1.isExpression(exps)) { | ||
const getValues = exports.compileExpression(exps); | ||
return (context) => { | ||
@@ -220,4 +205,8 @@ const v = getV(context); | ||
} | ||
case operator_1.Var: { | ||
const name = exp[1]; | ||
return (context) => context[name]; | ||
} | ||
} | ||
}; | ||
//# sourceMappingURL=compileExpression.js.map |
@@ -5,16 +5,3 @@ "use strict"; | ||
const operator_1 = require("./operator"); | ||
exports.evaluateOperand = (operand, context) => { | ||
if (common_1.isExpression(operand)) { | ||
return exports.evaluateExpression(operand, context); | ||
} | ||
if (typeof operand === 'string') { | ||
if (common_1.variablePattern.test(operand)) { | ||
return context[operand.substring(1)]; | ||
} | ||
if (operand.lastIndexOf('\\$', 0) === 0) { | ||
return operand.substring(1); | ||
} | ||
} | ||
return operand; | ||
}; | ||
exports.evaluateOperand = (operand, context) => common_1.isExpression(operand) ? exports.evaluateExpression(operand, context) : operand; | ||
exports.evaluateExpression = (exp, context) => { | ||
@@ -137,4 +124,4 @@ switch (exp[0]) { | ||
const exps = exp[2]; | ||
if (typeof exps === 'string') { | ||
const vs = exports.evaluateOperand(exps, context); | ||
if (common_1.isExpression(exps)) { | ||
const vs = exports.evaluateExpression(exps, context); | ||
if (Array.isArray(vs)) { | ||
@@ -160,4 +147,4 @@ for (let i = 0, len = vs.length; i < len; i += 1) { | ||
const exps = exp[2]; | ||
if (typeof exps === 'string') { | ||
const vs = exports.evaluateOperand(exps, context); | ||
if (common_1.isExpression(exps)) { | ||
const vs = exports.evaluateExpression(exps, context); | ||
if (Array.isArray(vs)) { | ||
@@ -180,4 +167,7 @@ for (let i = 0, len = vs.length; i < len; i += 1) { | ||
} | ||
case operator_1.Var: { | ||
return context[exp[1]]; | ||
} | ||
} | ||
}; | ||
//# sourceMappingURL=evaluateExpression.js.map |
@@ -1,7 +0,7 @@ | ||
export { isExpression, isOperator, variablePattern } from './common'; | ||
export { isExpression, isOperator } from './common'; | ||
export { EvaluateExpression, compileExpression, compileOperand, } from './compileExpression'; | ||
export { evaluateExpression, evaluateOperand } from './evaluateExpression'; | ||
export { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, } from './operator'; | ||
export { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, Var, } from './operator'; | ||
export { Operator, OperatorName, OperatorToName } from './operatorMap'; | ||
export { ExecutionContext, Expression, Expression1, Expression2, Expression3, Value, Value2, Value3, Value4, } from './ts'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,3 +6,2 @@ "use strict"; | ||
exports.isOperator = common_1.isOperator; | ||
exports.variablePattern = common_1.variablePattern; | ||
var compileExpression_1 = require("./compileExpression"); | ||
@@ -32,4 +31,5 @@ exports.compileExpression = compileExpression_1.compileExpression; | ||
exports.Subtract = operator_1.Subtract; | ||
exports.Var = operator_1.Var; | ||
var operatorMap_1 = require("./operatorMap"); | ||
exports.OperatorToName = operatorMap_1.OperatorToName; | ||
//# sourceMappingURL=index.js.map |
@@ -35,2 +35,4 @@ export declare type Equal = '=='; | ||
export declare const NotIn: NotIn; | ||
export declare type Var = '$'; | ||
export declare const Var: Var; | ||
//# sourceMappingURL=operator.d.ts.map |
@@ -20,2 +20,3 @@ "use strict"; | ||
exports.NotIn = '!In'; | ||
exports.Var = '$'; | ||
//# sourceMappingURL=operator.js.map |
@@ -22,3 +22,4 @@ import * as Operators from './operator'; | ||
'!In': 'NotIn'; | ||
$: 'Var'; | ||
}; | ||
//# sourceMappingURL=operatorMap.d.ts.map |
@@ -1,10 +0,10 @@ | ||
import { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract } from './operator'; | ||
import { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, Var } from './operator'; | ||
export declare type Value = null | boolean | number | string | Array<null | boolean | number | string>; | ||
export declare type Expression1 = [Equal, Value, Value] | [NotEqual, Value, Value] | [GreaterThan, Value, Value] | [GreaterThanOrEqual, Value, Value] | [LessThan, Value, Value] | [LessThanOrEqual, Value, Value] | [Add, Value, Value] | [Subtract, Value, Value] | [Multiply, Value, Value] | [Divide, Value, Value] | [Modulo, Value, Value] | [Not, Value] | [And, ...Value[]] | [Or, ...Value[]] | [IfThenElse, Value, Value, Value] | [In, Value, string | Value[]] | [NotIn, Value, string | Value[]]; | ||
export declare type Expression1 = [Equal, Value, Value] | [NotEqual, Value, Value] | [GreaterThan, Value, Value] | [GreaterThanOrEqual, Value, Value] | [LessThan, Value, Value] | [LessThanOrEqual, Value, Value] | [Add, Value, Value] | [Subtract, Value, Value] | [Multiply, Value, Value] | [Divide, Value, Value] | [Modulo, Value, Value] | [Not, Value] | [And, ...Value[]] | [Or, ...Value[]] | [IfThenElse, Value, Value, Value] | [In, Value, Value[]] | [NotIn, Value, Value[]] | [Var, string]; | ||
export declare type Value2 = Value | Expression1; | ||
export declare type Expression2 = [Equal, Value2, Value2] | [NotEqual, Value2, Value2] | [GreaterThan, Value2, Value2] | [GreaterThanOrEqual, Value2, Value2] | [LessThan, Value2, Value2] | [LessThanOrEqual, Value2, Value2] | [Add, Value2, Value2] | [Subtract, Value2, Value2] | [Multiply, Value2, Value2] | [Divide, Value2, Value2] | [Modulo, Value2, Value2] | [Not, Value2] | [And, ...Value2[]] | [Or, ...Value2[]] | [IfThenElse, Value2, Value2, Value2] | [In, Value2, string | Value2[]] | [NotIn, Value2, string | Value2[]]; | ||
export declare type Expression2 = [Equal, Value2, Value2] | [NotEqual, Value2, Value2] | [GreaterThan, Value2, Value2] | [GreaterThanOrEqual, Value2, Value2] | [LessThan, Value2, Value2] | [LessThanOrEqual, Value2, Value2] | [Add, Value2, Value2] | [Subtract, Value2, Value2] | [Multiply, Value2, Value2] | [Divide, Value2, Value2] | [Modulo, Value2, Value2] | [Not, Value2] | [And, ...Value2[]] | [Or, ...Value2[]] | [IfThenElse, Value2, Value2, Value2] | [In, Value2, Value2[]] | [NotIn, Value2, Value2[]] | [Var, string]; | ||
export declare type Value3 = Value | Expression1 | Value2 | Expression2; | ||
export declare type Expression3 = [Equal, Value3, Value3] | [NotEqual, Value3, Value3] | [GreaterThan, Value3, Value3] | [GreaterThanOrEqual, Value3, Value3] | [LessThan, Value3, Value3] | [LessThanOrEqual, Value3, Value3] | [Add, Value3, Value3] | [Subtract, Value3, Value3] | [Multiply, Value3, Value3] | [Divide, Value3, Value3] | [Modulo, Value3, Value3] | [Not, Value3] | [And, ...Value3[]] | [Or, ...Value3[]] | [IfThenElse, Value3, Value3, Value3] | [In, Value3, string | Value3[]] | [NotIn, Value3, string | Value3[]]; | ||
export declare type Expression3 = [Equal, Value3, Value3] | [NotEqual, Value3, Value3] | [GreaterThan, Value3, Value3] | [GreaterThanOrEqual, Value3, Value3] | [LessThan, Value3, Value3] | [LessThanOrEqual, Value3, Value3] | [Add, Value3, Value3] | [Subtract, Value3, Value3] | [Multiply, Value3, Value3] | [Divide, Value3, Value3] | [Modulo, Value3, Value3] | [Not, Value3] | [And, ...Value3[]] | [Or, ...Value3[]] | [IfThenElse, Value3, Value3, Value3] | [In, Value3, Value3[]] | [NotIn, Value3, Value3[]] | [Var, string]; | ||
export declare type Value4 = Value | Expression1 | Value2 | Expression2 | Value3 | Expression3; | ||
export declare type Expression = [Equal, Value4, Value4] | [NotEqual, Value4, Value4] | [GreaterThan, Value4, Value4] | [GreaterThanOrEqual, Value4, Value4] | [LessThan, Value4, Value4] | [LessThanOrEqual, Value4, Value4] | [Add, Value4, Value4] | [Subtract, Value4, Value4] | [Multiply, Value4, Value4] | [Divide, Value4, Value4] | [Modulo, Value4, Value4] | [Not, Value4] | [And, ...Value4[]] | [Or, ...Value4[]] | [IfThenElse, Value4, Value4, Value4] | [In, Value4, string | Value4[]] | [NotIn, Value4, string | Value4[]]; | ||
export declare type Expression = [Equal, Value4, Value4] | [NotEqual, Value4, Value4] | [GreaterThan, Value4, Value4] | [GreaterThanOrEqual, Value4, Value4] | [LessThan, Value4, Value4] | [LessThanOrEqual, Value4, Value4] | [Add, Value4, Value4] | [Subtract, Value4, Value4] | [Multiply, Value4, Value4] | [Divide, Value4, Value4] | [Modulo, Value4, Value4] | [Not, Value4] | [And, ...Value4[]] | [Or, ...Value4[]] | [IfThenElse, Value4, Value4, Value4] | [In, Value4, Value4[]] | [NotIn, Value4, Value4[]] | [Var, string]; | ||
export interface ExecutionContext { | ||
@@ -11,0 +11,0 @@ [key: string]: Value; |
import { Expression } from './ts'; | ||
export declare const variablePattern: RegExp; | ||
export declare const isOperator: (op: unknown) => op is "==" | "!=" | ">" | ">=" | "<" | "<=" | "+" | "-" | "*" | "/" | "%" | "!" | "&&" | "||" | "?:" | "In" | "!In"; | ||
export declare const isOperator: (op: unknown) => op is "==" | "!=" | ">" | ">=" | "<" | "<=" | "+" | "-" | "*" | "/" | "%" | "!" | "&&" | "||" | "?:" | "In" | "!In" | "$"; | ||
export declare const isExpression: (exp: unknown) => exp is Expression; | ||
//# sourceMappingURL=common.d.ts.map |
import { OperatorToName } from './operatorMap'; | ||
export const variablePattern = /^\$[a-z][\w-]*$/i; | ||
export const isOperator = (op) => typeof op === 'string' && OperatorToName.hasOwnProperty(op); | ||
export const isExpression = (exp) => Array.isArray(exp) && isOperator(exp[0]); | ||
//# sourceMappingURL=common.js.map |
@@ -1,19 +0,4 @@ | ||
import { isExpression, variablePattern } from './common'; | ||
import { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, } from './operator'; | ||
export const compileOperand = (operand) => { | ||
if (isExpression(operand)) { | ||
return compileExpression(operand); | ||
} | ||
if (typeof operand === 'string') { | ||
if (variablePattern.test(operand)) { | ||
const name = operand.substring(1); | ||
return (context) => context[name]; | ||
} | ||
if (operand.lastIndexOf('\\$', 0) === 0) { | ||
const unescaped = operand.substring(1); | ||
return () => unescaped; | ||
} | ||
} | ||
return () => operand; | ||
}; | ||
import { isExpression } from './common'; | ||
import { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, Var, } from './operator'; | ||
export const compileOperand = (operand) => isExpression(operand) ? compileExpression(operand) : () => operand; | ||
export const compileExpression = (exp) => { | ||
@@ -184,4 +169,4 @@ switch (exp[0]) { | ||
const exps = exp[2]; | ||
if (typeof exps === 'string') { | ||
const getValues = compileOperand(exps); | ||
if (isExpression(exps)) { | ||
const getValues = compileExpression(exps); | ||
return (context) => { | ||
@@ -217,4 +202,8 @@ const v = getV(context); | ||
} | ||
case Var: { | ||
const name = exp[1]; | ||
return (context) => context[name]; | ||
} | ||
} | ||
}; | ||
//# sourceMappingURL=compileExpression.js.map |
@@ -1,17 +0,4 @@ | ||
import { isExpression, variablePattern } from './common'; | ||
import { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, } from './operator'; | ||
export const evaluateOperand = (operand, context) => { | ||
if (isExpression(operand)) { | ||
return evaluateExpression(operand, context); | ||
} | ||
if (typeof operand === 'string') { | ||
if (variablePattern.test(operand)) { | ||
return context[operand.substring(1)]; | ||
} | ||
if (operand.lastIndexOf('\\$', 0) === 0) { | ||
return operand.substring(1); | ||
} | ||
} | ||
return operand; | ||
}; | ||
import { isExpression } from './common'; | ||
import { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, Var, } from './operator'; | ||
export const evaluateOperand = (operand, context) => isExpression(operand) ? evaluateExpression(operand, context) : operand; | ||
export const evaluateExpression = (exp, context) => { | ||
@@ -134,4 +121,4 @@ switch (exp[0]) { | ||
const exps = exp[2]; | ||
if (typeof exps === 'string') { | ||
const vs = evaluateOperand(exps, context); | ||
if (isExpression(exps)) { | ||
const vs = evaluateExpression(exps, context); | ||
if (Array.isArray(vs)) { | ||
@@ -157,4 +144,4 @@ for (let i = 0, len = vs.length; i < len; i += 1) { | ||
const exps = exp[2]; | ||
if (typeof exps === 'string') { | ||
const vs = evaluateOperand(exps, context); | ||
if (isExpression(exps)) { | ||
const vs = evaluateExpression(exps, context); | ||
if (Array.isArray(vs)) { | ||
@@ -177,4 +164,7 @@ for (let i = 0, len = vs.length; i < len; i += 1) { | ||
} | ||
case Var: { | ||
return context[exp[1]]; | ||
} | ||
} | ||
}; | ||
//# sourceMappingURL=evaluateExpression.js.map |
@@ -1,7 +0,7 @@ | ||
export { isExpression, isOperator, variablePattern } from './common'; | ||
export { isExpression, isOperator } from './common'; | ||
export { EvaluateExpression, compileExpression, compileOperand, } from './compileExpression'; | ||
export { evaluateExpression, evaluateOperand } from './evaluateExpression'; | ||
export { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, } from './operator'; | ||
export { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, Var, } from './operator'; | ||
export { Operator, OperatorName, OperatorToName } from './operatorMap'; | ||
export { ExecutionContext, Expression, Expression1, Expression2, Expression3, Value, Value2, Value3, Value4, } from './ts'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,6 +0,6 @@ | ||
export { isExpression, isOperator, variablePattern } from './common'; | ||
export { isExpression, isOperator } from './common'; | ||
export { compileExpression, compileOperand, } from './compileExpression'; | ||
export { evaluateExpression, evaluateOperand } from './evaluateExpression'; | ||
export { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, } from './operator'; | ||
export { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, Var, } from './operator'; | ||
export { OperatorToName } from './operatorMap'; | ||
//# sourceMappingURL=index.js.map |
@@ -35,2 +35,4 @@ export declare type Equal = '=='; | ||
export declare const NotIn: NotIn; | ||
export declare type Var = '$'; | ||
export declare const Var: Var; | ||
//# sourceMappingURL=operator.d.ts.map |
@@ -18,2 +18,3 @@ export const Equal = '=='; | ||
export const NotIn = '!In'; | ||
export const Var = '$'; | ||
//# sourceMappingURL=operator.js.map |
@@ -22,3 +22,4 @@ import * as Operators from './operator'; | ||
'!In': 'NotIn'; | ||
$: 'Var'; | ||
}; | ||
//# sourceMappingURL=operatorMap.d.ts.map |
@@ -1,10 +0,10 @@ | ||
import { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract } from './operator'; | ||
import { Add, And, Divide, Equal, GreaterThan, GreaterThanOrEqual, IfThenElse, In, LessThan, LessThanOrEqual, Modulo, Multiply, Not, NotEqual, NotIn, Or, Subtract, Var } from './operator'; | ||
export declare type Value = null | boolean | number | string | Array<null | boolean | number | string>; | ||
export declare type Expression1 = [Equal, Value, Value] | [NotEqual, Value, Value] | [GreaterThan, Value, Value] | [GreaterThanOrEqual, Value, Value] | [LessThan, Value, Value] | [LessThanOrEqual, Value, Value] | [Add, Value, Value] | [Subtract, Value, Value] | [Multiply, Value, Value] | [Divide, Value, Value] | [Modulo, Value, Value] | [Not, Value] | [And, ...Value[]] | [Or, ...Value[]] | [IfThenElse, Value, Value, Value] | [In, Value, string | Value[]] | [NotIn, Value, string | Value[]]; | ||
export declare type Expression1 = [Equal, Value, Value] | [NotEqual, Value, Value] | [GreaterThan, Value, Value] | [GreaterThanOrEqual, Value, Value] | [LessThan, Value, Value] | [LessThanOrEqual, Value, Value] | [Add, Value, Value] | [Subtract, Value, Value] | [Multiply, Value, Value] | [Divide, Value, Value] | [Modulo, Value, Value] | [Not, Value] | [And, ...Value[]] | [Or, ...Value[]] | [IfThenElse, Value, Value, Value] | [In, Value, Value[]] | [NotIn, Value, Value[]] | [Var, string]; | ||
export declare type Value2 = Value | Expression1; | ||
export declare type Expression2 = [Equal, Value2, Value2] | [NotEqual, Value2, Value2] | [GreaterThan, Value2, Value2] | [GreaterThanOrEqual, Value2, Value2] | [LessThan, Value2, Value2] | [LessThanOrEqual, Value2, Value2] | [Add, Value2, Value2] | [Subtract, Value2, Value2] | [Multiply, Value2, Value2] | [Divide, Value2, Value2] | [Modulo, Value2, Value2] | [Not, Value2] | [And, ...Value2[]] | [Or, ...Value2[]] | [IfThenElse, Value2, Value2, Value2] | [In, Value2, string | Value2[]] | [NotIn, Value2, string | Value2[]]; | ||
export declare type Expression2 = [Equal, Value2, Value2] | [NotEqual, Value2, Value2] | [GreaterThan, Value2, Value2] | [GreaterThanOrEqual, Value2, Value2] | [LessThan, Value2, Value2] | [LessThanOrEqual, Value2, Value2] | [Add, Value2, Value2] | [Subtract, Value2, Value2] | [Multiply, Value2, Value2] | [Divide, Value2, Value2] | [Modulo, Value2, Value2] | [Not, Value2] | [And, ...Value2[]] | [Or, ...Value2[]] | [IfThenElse, Value2, Value2, Value2] | [In, Value2, Value2[]] | [NotIn, Value2, Value2[]] | [Var, string]; | ||
export declare type Value3 = Value | Expression1 | Value2 | Expression2; | ||
export declare type Expression3 = [Equal, Value3, Value3] | [NotEqual, Value3, Value3] | [GreaterThan, Value3, Value3] | [GreaterThanOrEqual, Value3, Value3] | [LessThan, Value3, Value3] | [LessThanOrEqual, Value3, Value3] | [Add, Value3, Value3] | [Subtract, Value3, Value3] | [Multiply, Value3, Value3] | [Divide, Value3, Value3] | [Modulo, Value3, Value3] | [Not, Value3] | [And, ...Value3[]] | [Or, ...Value3[]] | [IfThenElse, Value3, Value3, Value3] | [In, Value3, string | Value3[]] | [NotIn, Value3, string | Value3[]]; | ||
export declare type Expression3 = [Equal, Value3, Value3] | [NotEqual, Value3, Value3] | [GreaterThan, Value3, Value3] | [GreaterThanOrEqual, Value3, Value3] | [LessThan, Value3, Value3] | [LessThanOrEqual, Value3, Value3] | [Add, Value3, Value3] | [Subtract, Value3, Value3] | [Multiply, Value3, Value3] | [Divide, Value3, Value3] | [Modulo, Value3, Value3] | [Not, Value3] | [And, ...Value3[]] | [Or, ...Value3[]] | [IfThenElse, Value3, Value3, Value3] | [In, Value3, Value3[]] | [NotIn, Value3, Value3[]] | [Var, string]; | ||
export declare type Value4 = Value | Expression1 | Value2 | Expression2 | Value3 | Expression3; | ||
export declare type Expression = [Equal, Value4, Value4] | [NotEqual, Value4, Value4] | [GreaterThan, Value4, Value4] | [GreaterThanOrEqual, Value4, Value4] | [LessThan, Value4, Value4] | [LessThanOrEqual, Value4, Value4] | [Add, Value4, Value4] | [Subtract, Value4, Value4] | [Multiply, Value4, Value4] | [Divide, Value4, Value4] | [Modulo, Value4, Value4] | [Not, Value4] | [And, ...Value4[]] | [Or, ...Value4[]] | [IfThenElse, Value4, Value4, Value4] | [In, Value4, string | Value4[]] | [NotIn, Value4, string | Value4[]]; | ||
export declare type Expression = [Equal, Value4, Value4] | [NotEqual, Value4, Value4] | [GreaterThan, Value4, Value4] | [GreaterThanOrEqual, Value4, Value4] | [LessThan, Value4, Value4] | [LessThanOrEqual, Value4, Value4] | [Add, Value4, Value4] | [Subtract, Value4, Value4] | [Multiply, Value4, Value4] | [Divide, Value4, Value4] | [Modulo, Value4, Value4] | [Not, Value4] | [And, ...Value4[]] | [Or, ...Value4[]] | [IfThenElse, Value4, Value4, Value4] | [In, Value4, Value4[]] | [NotIn, Value4, Value4[]] | [Var, string]; | ||
export interface ExecutionContext { | ||
@@ -11,0 +11,0 @@ [key: string]: Value; |
{ | ||
"name": "expjson", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Super lightweight, fast, and optimized evaluate-able and compilable expressions in JSON written in TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
175
README.md
@@ -28,92 +28,117 @@ # expjson | ||
```typescript | ||
import { compileExpression, evaluateExpression, IfThenElse, In, Not } from 'expjson'; | ||
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); | ||
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 | ||
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'); | ||
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' | ||
// compileExpression: compile then evaluate | ||
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" | ||
[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); | ||
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" | ||
['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); | ||
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" | ||
[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" | ||
]); | ||
compiled3({ | ||
postDeleted: false, | ||
roles: ['user', 'guest'], | ||
unauthorized: 'Unauthorized Error', | ||
}) === 'Unauthorized Error' | ||
expect( | ||
compiled3({ | ||
postDeleted: false, | ||
roles: ['user', 'guest'], | ||
unauthorized: 'Unauthorized Error', | ||
}) | ||
).toBe('Unauthorized Error'); | ||
``` | ||
@@ -120,0 +145,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
147
146856
1576