ts-expression-evaluator
Advanced tools
Comparing version 1.3.5 to 1.3.6
import * as t from './t'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression' | 'LogicalExpression'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression' | 'LogicalExpression' | 'UnaryExpression' | 'ThisExpression'; | ||
export declare type Context = { | ||
@@ -4,0 +4,0 @@ [key: string]: any; |
@@ -56,2 +56,11 @@ "use strict"; | ||
}, | ||
UnaryExpression: function (ast, context) { | ||
if (t.isUnaryExpression(ast)) { | ||
switch (ast.operator) { | ||
case '!': | ||
return !Evaluator_1.evaluate(ast.argument, context); | ||
} | ||
} | ||
throw new Error(); | ||
}, | ||
Identifier: function (ast, context) { | ||
@@ -91,5 +100,14 @@ if (t.isIdentifier(ast)) { | ||
} | ||
if ((t.isBinaryExpression(ast.property) || t.isLogicalExpression(ast.property)) && typeof obj.filter === 'function') { | ||
return obj.filter(function (item) { return Evaluator_1.evaluate(ast.property, { context: context, __scope: item }); }); | ||
} | ||
} | ||
throw new Error(); | ||
}, | ||
ThisExpression: function (ast, context) { | ||
if (t.isThisExpression(ast)) { | ||
return context.__scope; | ||
} | ||
throw new Error(); | ||
}, | ||
NumericLiteral: function (ast) { | ||
@@ -96,0 +114,0 @@ if (t.isNumericLiteral(ast)) { |
@@ -12,2 +12,4 @@ import * as t from '@babel/types'; | ||
export declare const isLogicalExpression: (ast: t.Expression) => ast is t.LogicalExpression; | ||
export declare const isUnaryExpression: (ast: t.Expression) => ast is t.UnaryExpression; | ||
export declare const isThisExpression: (ast: t.Expression) => ast is t.ThisExpression; | ||
export declare type Expression = t.Expression; |
@@ -33,1 +33,7 @@ "use strict"; | ||
}; | ||
exports.isUnaryExpression = function (ast) { | ||
return ast.type === 'UnaryExpression'; | ||
}; | ||
exports.isThisExpression = function (ast) { | ||
return ast.type === 'ThisExpression'; | ||
}; |
import * as t from './t'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression' | 'LogicalExpression'; | ||
export declare type HandlerTypes = 'BinaryExpression' | 'NumericLiteral' | 'StringLiteral' | 'BooleanLiteral' | 'ArrayExpression' | 'NullLiteral' | 'Identifier' | 'CallExpression' | 'MemberExpression' | 'LogicalExpression' | 'UnaryExpression' | 'ThisExpression'; | ||
export declare type Context = { | ||
@@ -4,0 +4,0 @@ [key: string]: any; |
@@ -47,2 +47,11 @@ import * as t from './t'; | ||
}, | ||
UnaryExpression: function (ast, context) { | ||
if (t.isUnaryExpression(ast)) { | ||
switch (ast.operator) { | ||
case '!': | ||
return !evaluate(ast.argument, context); | ||
} | ||
} | ||
throw new Error(); | ||
}, | ||
Identifier: function (ast, context) { | ||
@@ -82,5 +91,14 @@ if (t.isIdentifier(ast)) { | ||
} | ||
if ((t.isBinaryExpression(ast.property) || t.isLogicalExpression(ast.property)) && typeof obj.filter === 'function') { | ||
return obj.filter(function (item) { return evaluate(ast.property, { context: context, __scope: item }); }); | ||
} | ||
} | ||
throw new Error(); | ||
}, | ||
ThisExpression: function (ast, context) { | ||
if (t.isThisExpression(ast)) { | ||
return context.__scope; | ||
} | ||
throw new Error(); | ||
}, | ||
NumericLiteral: function (ast) { | ||
@@ -87,0 +105,0 @@ if (t.isNumericLiteral(ast)) { |
@@ -12,2 +12,4 @@ import * as t from '@babel/types'; | ||
export declare const isLogicalExpression: (ast: t.Expression) => ast is t.LogicalExpression; | ||
export declare const isUnaryExpression: (ast: t.Expression) => ast is t.UnaryExpression; | ||
export declare const isThisExpression: (ast: t.Expression) => ast is t.ThisExpression; | ||
export declare type Expression = t.Expression; |
@@ -31,1 +31,7 @@ export var isIdentifier = function (ast) { | ||
}; | ||
export var isUnaryExpression = function (ast) { | ||
return ast.type === 'UnaryExpression'; | ||
}; | ||
export var isThisExpression = function (ast) { | ||
return ast.type === 'ThisExpression'; | ||
}; |
{ | ||
"name": "ts-expression-evaluator", | ||
"version": "1.3.5", | ||
"version": "1.3.6", | ||
"description": "Context-based expression parse and evaluator.", | ||
@@ -70,3 +70,3 @@ "main": "build/main/index.js", | ||
"trash-cli": "^1.4.0", | ||
"ts-jest": "^23.1.4", | ||
"ts-jest": "^23.10.2", | ||
"tslint": "^5.10.0", | ||
@@ -90,6 +90,3 @@ "tslint-config-prettier": "^1.13.0", | ||
], | ||
"transform": { | ||
"^.+\\.ts?$": "ts-jest" | ||
}, | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.ts$", | ||
"preset": "ts-jest", | ||
"moduleFileExtensions": [ | ||
@@ -99,9 +96,10 @@ "ts", | ||
"json" | ||
] | ||
}, | ||
"globals": { | ||
"ts-jest": { | ||
"tsConfigFile": "tsconfig.test.json" | ||
], | ||
"globals": { | ||
"ts-jest": { | ||
"tsConfig": "tsconfig.test.json", | ||
"diagnostics": false | ||
} | ||
} | ||
} | ||
} | ||
} |
@@ -55,4 +55,48 @@ # ts-expression-evaluator | ||
// array filter | ||
evaluate('staffs[this.id === 1]', context) // [{id: 1, name: 'Tina'}] | ||
``` | ||
## Details | ||
### Unary Operators | ||
| Operation | Symbol | | ||
|-----------|:------:| | ||
| Negate | ! | | ||
### Binary Operators | ||
| Operation | Symbol | | ||
|------------------|:----------------:| | ||
| Add, Concat | + | | ||
| Subtract | - | | ||
| Multiply | * | | ||
| Divide | / | | ||
### Logical Operators | ||
| Operation | Symbol | | ||
|------------------|:----------------:| | ||
| Logical AND | && | | ||
| Logical OR | || | | ||
### Comparisons | ||
| Comparison | Symbol | | ||
|----------------------------|:------:| | ||
| Equal | == | | ||
| Not equal | != | | ||
| Greater than | > | | ||
| Greater than or equal | >= | | ||
| Less than | < | | ||
| Less than or equal | <= | | ||
### Native Types | ||
| Type | Examples | | ||
|----------|:------------------------------:| | ||
| Booleans | `true`, `false` | | ||
| Strings | "Hello \"user\"", 'Hey there!' | | ||
| Numerics | 6, -7.2, 5, -3.14159 | | ||
| Arrays | ['hello', 'world!'] | | ||
## API | ||
@@ -59,0 +103,0 @@ ### `evaluate(exp: string, context: object): any` |
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
192682
512
110