ts-expression-evaluator
Advanced tools
Comparing version 1.2.7 to 1.3.0
import { Context } from './lib/Handlers'; | ||
export { registerFunction, registerFunctions } from './lib/functions'; | ||
declare const _default: (code: string, context: Context) => any; | ||
declare const _default: (code: string, context?: Context) => any; | ||
export default _default; |
@@ -8,3 +8,4 @@ "use strict"; | ||
exports.default = (function (code, context) { | ||
if (context === void 0) { context = {}; } | ||
return Evaluator_1.evaluate(code, 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.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; |
@@ -6,3 +6,2 @@ "use strict"; | ||
exports.evaluate = function (code, context) { | ||
if (context === void 0) { context = {}; } | ||
if (typeof code === 'string') { | ||
@@ -9,0 +8,0 @@ var ast = parser_1.parseExpression(code); |
import * as t from './t'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression' | 'LogicalExpression'; | ||
export declare type Context = { | ||
@@ -4,0 +4,0 @@ [key: string]: any; |
@@ -33,2 +33,10 @@ "use strict"; | ||
return Evaluator_1.evaluate(ast.left, context) != Evaluator_1.evaluate(ast.right, context); | ||
case '>': | ||
return Evaluator_1.evaluate(ast.left, context) > Evaluator_1.evaluate(ast.right, context); | ||
case '>=': | ||
return Evaluator_1.evaluate(ast.left, context) >= Evaluator_1.evaluate(ast.right, context); | ||
case '<': | ||
return Evaluator_1.evaluate(ast.left, context) < Evaluator_1.evaluate(ast.right, context); | ||
case '<=': | ||
return Evaluator_1.evaluate(ast.left, context) <= Evaluator_1.evaluate(ast.right, context); | ||
} | ||
@@ -38,2 +46,13 @@ } | ||
}, | ||
LogicalExpression: function (ast, context) { | ||
if (t.isLogicalExpression(ast)) { | ||
switch (ast.operator) { | ||
case '&&': | ||
return Evaluator_1.evaluate(ast.left, context) && Evaluator_1.evaluate(ast.right, context); | ||
case '||': | ||
return Evaluator_1.evaluate(ast.left, context) || Evaluator_1.evaluate(ast.right, context); | ||
} | ||
} | ||
throw new Error(); | ||
}, | ||
Identifier: function (ast, context) { | ||
@@ -73,3 +92,2 @@ if (t.isIdentifier(ast)) { | ||
} | ||
return Evaluator_1.evaluate(ast.property, context); | ||
} | ||
@@ -105,4 +123,2 @@ throw new Error(); | ||
return ast.elements.map(function (elem) { | ||
if (!elem) | ||
throw new Error(); | ||
return Evaluator_1.evaluate(elem, context); | ||
@@ -109,0 +125,0 @@ }); |
@@ -11,2 +11,3 @@ import * as t from '@babel/types'; | ||
export declare const isCallExpression: (ast: t.Expression) => ast is t.CallExpression; | ||
export declare const isLogicalExpression: (ast: t.Expression) => ast is t.LogicalExpression; | ||
export declare type Expression = t.Expression; |
@@ -30,1 +30,4 @@ "use strict"; | ||
}; | ||
exports.isLogicalExpression = function (ast) { | ||
return ast.type === 'LogicalExpression'; | ||
}; |
import { Context } from './lib/Handlers'; | ||
export { registerFunction, registerFunctions } from './lib/functions'; | ||
declare const _default: (code: string, context: Context) => any; | ||
declare const _default: (code: string, context?: Context) => any; | ||
export default _default; |
import { evaluate } from './lib/Evaluator'; | ||
export { registerFunction, registerFunctions } from './lib/functions'; | ||
export default (function (code, context) { | ||
if (context === void 0) { context = {}; } | ||
return evaluate(code, 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.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; |
import { parseExpression } from '@babel/parser'; | ||
import { Handlers } from './Handlers'; | ||
export var evaluate = function (code, context) { | ||
if (context === void 0) { context = {}; } | ||
if (typeof code === 'string') { | ||
@@ -6,0 +5,0 @@ var ast = parseExpression(code); |
import * as t from './t'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression' | 'LogicalExpression'; | ||
export declare type Context = { | ||
@@ -4,0 +4,0 @@ [key: string]: any; |
@@ -24,2 +24,10 @@ import * as t from './t'; | ||
return evaluate(ast.left, context) != evaluate(ast.right, context); | ||
case '>': | ||
return evaluate(ast.left, context) > evaluate(ast.right, context); | ||
case '>=': | ||
return evaluate(ast.left, context) >= evaluate(ast.right, context); | ||
case '<': | ||
return evaluate(ast.left, context) < evaluate(ast.right, context); | ||
case '<=': | ||
return evaluate(ast.left, context) <= evaluate(ast.right, context); | ||
} | ||
@@ -29,2 +37,13 @@ } | ||
}, | ||
LogicalExpression: function (ast, context) { | ||
if (t.isLogicalExpression(ast)) { | ||
switch (ast.operator) { | ||
case '&&': | ||
return evaluate(ast.left, context) && evaluate(ast.right, context); | ||
case '||': | ||
return evaluate(ast.left, context) || evaluate(ast.right, context); | ||
} | ||
} | ||
throw new Error(); | ||
}, | ||
Identifier: function (ast, context) { | ||
@@ -64,3 +83,2 @@ if (t.isIdentifier(ast)) { | ||
} | ||
return evaluate(ast.property, context); | ||
} | ||
@@ -96,4 +114,2 @@ throw new Error(); | ||
return ast.elements.map(function (elem) { | ||
if (!elem) | ||
throw new Error(); | ||
return evaluate(elem, context); | ||
@@ -100,0 +116,0 @@ }); |
@@ -11,2 +11,3 @@ import * as t from '@babel/types'; | ||
export declare const isCallExpression: (ast: t.Expression) => ast is t.CallExpression; | ||
export declare const isLogicalExpression: (ast: t.Expression) => ast is t.LogicalExpression; | ||
export declare type Expression = t.Expression; |
@@ -28,1 +28,4 @@ export var isIdentifier = function (ast) { | ||
}; | ||
export var isLogicalExpression = function (ast) { | ||
return ast.type === 'LogicalExpression'; | ||
}; |
{ | ||
"name": "ts-expression-evaluator", | ||
"version": "1.2.7", | ||
"version": "1.3.0", | ||
"description": "Context-based expression parse and evaluator.", | ||
@@ -25,8 +25,4 @@ "main": "build/main/index.js", | ||
"test:lint": "tslint --project . && prettier \"src/**/*.ts\" --list-different", | ||
"test:unit": "nyc --silent ava", | ||
"watch": "run-s clean build:main && run-p \"build:main -- -w\" \"test:unit -- --watch\"", | ||
"cov": "run-s build test:unit cov:html && opn coverage/index.html", | ||
"cov:html": "nyc report --reporter=html", | ||
"cov:send": "nyc report --reporter=lcov > coverage.lcov && codecov", | ||
"cov:check": "nyc report && nyc check-coverage --lines 100 --functions 100 --branches 100", | ||
"test:unit": "jest --watch", | ||
"cov": "jest --coverage && opn coverage/lcov-report/index.html", | ||
"doc": "run-s doc:html && opn build/docs/index.html", | ||
@@ -62,9 +58,10 @@ "doc:html": "typedoc src/ --target ES6 --mode file --out build/docs", | ||
"devDependencies": { | ||
"@babel/types": "^7.0.0", | ||
"@types/babel__parser": "^7.0.0", | ||
"ava": "1.0.0-beta.7", | ||
"@types/jest": "^23.3.2", | ||
"codecov": "^3.0.2", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"gh-pages": "^1.2.0", | ||
"jest": "^23.6.0", | ||
"npm-run-all": "^4.1.3", | ||
"nyc": "^12.0.2", | ||
"opn-cli": "^3.1.0", | ||
@@ -74,2 +71,3 @@ "prettier": "^1.13.4", | ||
"trash-cli": "^1.4.0", | ||
"ts-jest": "^23.1.4", | ||
"tslint": "^5.10.0", | ||
@@ -81,11 +79,2 @@ "tslint-config-prettier": "^1.13.0", | ||
}, | ||
"ava": { | ||
"failFast": true, | ||
"files": [ | ||
"build/main/**/*.spec.js" | ||
], | ||
"sources": [ | ||
"build/main/**/*.js" | ||
] | ||
}, | ||
"config": { | ||
@@ -99,7 +88,21 @@ "commitizen": { | ||
}, | ||
"nyc": { | ||
"exclude": [ | ||
"**/*.spec.js" | ||
"jest": { | ||
"collectCoverageFrom": [ | ||
"src/**/*.ts" | ||
], | ||
"transform": { | ||
"^.+\\.ts?$": "ts-jest" | ||
}, | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.ts$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"js", | ||
"json" | ||
] | ||
}, | ||
"globals": { | ||
"ts-jest": { | ||
"tsConfigFile": "tsconfig.test.json" | ||
} | ||
} | ||
} |
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
25
460
187608
18