ts-expression-evaluator
Advanced tools
Comparing version 1.3.13 to 1.3.14
@@ -5,4 +5,4 @@ "use strict"; | ||
var functions_1 = require("./lib/functions"); | ||
exports.registerFunction = functions_1.registerFunction; | ||
exports.registerFunctions = functions_1.registerFunctions; | ||
Object.defineProperty(exports, "registerFunction", { enumerable: true, get: function () { return functions_1.registerFunction; } }); | ||
Object.defineProperty(exports, "registerFunctions", { enumerable: true, get: function () { return functions_1.registerFunctions; } }); | ||
exports.default = (function (code, context) { | ||
@@ -9,0 +9,0 @@ if (context === void 0) { context = {}; } |
import * as t from '@babel/types'; | ||
export declare const evaluate: (code: string | t.ArrayExpression | t.ArrowFunctionExpression | t.AssignmentExpression | t.AwaitExpression | t.BigIntLiteral | t.BinaryExpression | t.LogicalExpression | t.BindExpression | t.FunctionExpression | t.BooleanLiteral | t.CallExpression | t.ClassExpression | t.ConditionalExpression | t.DoExpression | t.Identifier | t.StringLiteral | t.NumericLiteral | t.NullLiteral | t.RegExpLiteral | t.MemberExpression | t.NewExpression | t.ObjectExpression | t.SequenceExpression | t.ThisExpression | t.UnaryExpression | t.UpdateExpression | t.MetaProperty | t.Super | t.TaggedTemplateExpression | t.TemplateLiteral | t.YieldExpression | t.TypeCastExpression | t.JSXElement | t.JSXFragment | t.ParenthesizedExpression | t.OptionalMemberExpression | t.OptionalCallExpression | t.Import | t.TSAsExpression | t.TSTypeAssertion | t.TSNonNullExpression, context: {}) => any; | ||
export declare const evaluate: (code: string | t.Expression, context: {}) => any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.evaluate = void 0; | ||
var parser_1 = require("@babel/parser"); | ||
@@ -4,0 +5,0 @@ var Handlers_1 = require("./Handlers"); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getFunction = exports.registerFunctions = exports.registerFunction = void 0; | ||
var functions = {}; | ||
@@ -4,0 +5,0 @@ exports.registerFunction = function (name, func) { |
import * as t from './t'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression' | 'LogicalExpression' | 'UnaryExpression' | 'ThisExpression'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression' | 'LogicalExpression' | 'UnaryExpression' | 'ThisExpression' | 'ConditionalExpression'; | ||
export declare type Context = { | ||
@@ -4,0 +4,0 @@ [key: string]: any; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Handlers = void 0; | ||
var t = __importStar(require("./t")); | ||
@@ -41,2 +61,11 @@ var Evaluator_1 = require("./Evaluator"); | ||
return Evaluator_1.evaluate(ast.left, context) <= Evaluator_1.evaluate(ast.right, context); | ||
case 'in': { | ||
var right = Evaluator_1.evaluate(ast.right, context); | ||
if (!right) | ||
return false; | ||
if (typeof right === 'object' && right instanceof Array) { | ||
return right.indexOf(Evaluator_1.evaluate(ast.left, context)) !== -1; | ||
} | ||
return false; | ||
} | ||
} | ||
@@ -46,2 +75,8 @@ } | ||
}, | ||
ConditionalExpression: function (ast, context) { | ||
if (t.isConditionalExpression(ast)) { | ||
return Evaluator_1.evaluate(ast.test, context) ? Evaluator_1.evaluate(ast.consequent, context) : Evaluator_1.evaluate(ast.alternate, context); | ||
} | ||
throw new Error(); | ||
}, | ||
LogicalExpression: function (ast, context) { | ||
@@ -63,2 +98,4 @@ if (t.isLogicalExpression(ast)) { | ||
return !Evaluator_1.evaluate(ast.argument, context); | ||
case '-': | ||
return -Evaluator_1.evaluate(ast.argument, context); | ||
} | ||
@@ -81,3 +118,3 @@ } | ||
if (t.isCallExpression(ast)) { | ||
if (t.isIdentifier(ast.callee)) { | ||
if (!t.isV8IntrinsicIdentifier(ast.callee) && t.isIdentifier(ast.callee)) { | ||
var func = functions_1.getFunction(ast.callee.name); | ||
@@ -87,3 +124,3 @@ var args = ast.arguments.map(function (arg) { | ||
}); | ||
return func.call.apply(func, [null].concat(args)); | ||
return func.call.apply(func, __spreadArrays([null], args)); | ||
} | ||
@@ -101,2 +138,5 @@ } | ||
} | ||
if (t.isMemberExpression(ast.property)) { | ||
return obj[Evaluator_1.evaluate(ast.property, context)]; | ||
} | ||
if (t.isNumericLiteral(ast.property) || t.isStringLiteral(ast.property)) { | ||
@@ -103,0 +143,0 @@ return obj[ast.property.value]; |
@@ -14,2 +14,4 @@ import * as t from '@babel/types'; | ||
export declare const isThisExpression: (ast: t.Expression) => ast is t.ThisExpression; | ||
export declare const isConditionalExpression: (ast: t.Expression) => ast is t.ConditionalExpression; | ||
export declare const isV8IntrinsicIdentifier: (node: t.Expression | t.V8IntrinsicIdentifier) => node is t.V8IntrinsicIdentifier; | ||
export declare type Expression = t.Expression; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isV8IntrinsicIdentifier = exports.isConditionalExpression = exports.isThisExpression = exports.isUnaryExpression = exports.isLogicalExpression = exports.isCallExpression = exports.isArrayExpression = exports.isNullLiteral = exports.isBooleanLiteral = exports.isStringLiteral = exports.isNumericLiteral = exports.isMemberExpression = exports.isBinaryExpression = exports.isIdentifier = void 0; | ||
exports.isIdentifier = function (ast) { | ||
@@ -39,1 +40,7 @@ return ast.type === 'Identifier'; | ||
}; | ||
exports.isConditionalExpression = function (ast) { | ||
return ast.type === 'ConditionalExpression'; | ||
}; | ||
exports.isV8IntrinsicIdentifier = function (node) { | ||
return node.type === 'V8IntrinsicIdentifier'; | ||
}; |
import * as t from '@babel/types'; | ||
export declare const evaluate: (code: string | t.ArrayExpression | t.ArrowFunctionExpression | t.AssignmentExpression | t.AwaitExpression | t.BigIntLiteral | t.BinaryExpression | t.LogicalExpression | t.BindExpression | t.FunctionExpression | t.BooleanLiteral | t.CallExpression | t.ClassExpression | t.ConditionalExpression | t.DoExpression | t.Identifier | t.StringLiteral | t.NumericLiteral | t.NullLiteral | t.RegExpLiteral | t.MemberExpression | t.NewExpression | t.ObjectExpression | t.SequenceExpression | t.ThisExpression | t.UnaryExpression | t.UpdateExpression | t.MetaProperty | t.Super | t.TaggedTemplateExpression | t.TemplateLiteral | t.YieldExpression | t.TypeCastExpression | t.JSXElement | t.JSXFragment | t.ParenthesizedExpression | t.OptionalMemberExpression | t.OptionalCallExpression | t.Import | t.TSAsExpression | t.TSTypeAssertion | t.TSNonNullExpression, context: {}) => any; | ||
export declare const evaluate: (code: string | t.Expression, context: {}) => any; |
import * as t from './t'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression' | 'LogicalExpression' | 'UnaryExpression' | 'ThisExpression'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression' | 'LogicalExpression' | 'UnaryExpression' | 'ThisExpression' | 'ConditionalExpression'; | ||
export declare type Context = { | ||
@@ -4,0 +4,0 @@ [key: string]: any; |
@@ -0,1 +1,8 @@ | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
import * as t from './t'; | ||
@@ -32,2 +39,11 @@ import { evaluate } from './Evaluator'; | ||
return evaluate(ast.left, context) <= evaluate(ast.right, context); | ||
case 'in': { | ||
var right = evaluate(ast.right, context); | ||
if (!right) | ||
return false; | ||
if (typeof right === 'object' && right instanceof Array) { | ||
return right.indexOf(evaluate(ast.left, context)) !== -1; | ||
} | ||
return false; | ||
} | ||
} | ||
@@ -37,2 +53,8 @@ } | ||
}, | ||
ConditionalExpression: function (ast, context) { | ||
if (t.isConditionalExpression(ast)) { | ||
return evaluate(ast.test, context) ? evaluate(ast.consequent, context) : evaluate(ast.alternate, context); | ||
} | ||
throw new Error(); | ||
}, | ||
LogicalExpression: function (ast, context) { | ||
@@ -54,2 +76,4 @@ if (t.isLogicalExpression(ast)) { | ||
return !evaluate(ast.argument, context); | ||
case '-': | ||
return -evaluate(ast.argument, context); | ||
} | ||
@@ -72,3 +96,3 @@ } | ||
if (t.isCallExpression(ast)) { | ||
if (t.isIdentifier(ast.callee)) { | ||
if (!t.isV8IntrinsicIdentifier(ast.callee) && t.isIdentifier(ast.callee)) { | ||
var func = getFunction(ast.callee.name); | ||
@@ -78,3 +102,3 @@ var args = ast.arguments.map(function (arg) { | ||
}); | ||
return func.call.apply(func, [null].concat(args)); | ||
return func.call.apply(func, __spreadArrays([null], args)); | ||
} | ||
@@ -92,2 +116,5 @@ } | ||
} | ||
if (t.isMemberExpression(ast.property)) { | ||
return obj[evaluate(ast.property, context)]; | ||
} | ||
if (t.isNumericLiteral(ast.property) || t.isStringLiteral(ast.property)) { | ||
@@ -94,0 +121,0 @@ return obj[ast.property.value]; |
@@ -14,2 +14,4 @@ import * as t from '@babel/types'; | ||
export declare const isThisExpression: (ast: t.Expression) => ast is t.ThisExpression; | ||
export declare const isConditionalExpression: (ast: t.Expression) => ast is t.ConditionalExpression; | ||
export declare const isV8IntrinsicIdentifier: (node: t.Expression | t.V8IntrinsicIdentifier) => node is t.V8IntrinsicIdentifier; | ||
export declare type Expression = t.Expression; |
@@ -37,1 +37,7 @@ export var isIdentifier = function (ast) { | ||
}; | ||
export var isConditionalExpression = function (ast) { | ||
return ast.type === 'ConditionalExpression'; | ||
}; | ||
export var isV8IntrinsicIdentifier = function (node) { | ||
return node.type === 'V8IntrinsicIdentifier'; | ||
}; |
{ | ||
"name": "ts-expression-evaluator", | ||
"version": "1.3.13", | ||
"version": "1.3.14", | ||
"description": "Context-based expression parse and evaluator.", | ||
@@ -102,2 +102,2 @@ "main": "build/main/index.js", | ||
} | ||
} | ||
} |
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
595
32184
24