1.3.14 • Published 4 years ago
ts-expression-evaluator v1.3.14
ts-expression-evaluator
Context-based expression evaluator.
install
- npm
npm install ts-expression-evaluator
- yarn
yarn add ts-expression-evaluator
Quick start
import evaluate, { registerFunction } from 'ts-expression-evaluator'
const context = {
id: 10,
name: 'FED',
count: 10,
staffs: [{
id: 1,
name: 'Tina'
}, {
id: 2,
name: 'James'
}],
}
// math
evaluate('1+2') // 3
// eval from context
evaluate('name', context) // 'FED'
evaluate('staffs[0].name', context) // 'Tina'
evaluate('count + 10', context) // 20
// ===
evaluate('count === 11', context) // false
// register custom function
registerFunction('IF', (condition, ifTrue, ifFalse) => {
return condition ? ifTrue : ifFalse;
})
evaluate('IF(name === 'FED', 'It\'s FED.', 'It\'s not FED.')') // It's FED.
// 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
evaluate(exp: string, context: object): any
registerFunction(name: string, func: Function): void
registerFunctions(funcs: {[key: string]: Function}): void
License
MIT
1.3.14
4 years ago
1.3.13
5 years ago
1.3.12
5 years ago
1.3.11
6 years ago
1.3.10
6 years ago
1.3.9
6 years ago
1.3.8
6 years ago
1.3.7
6 years ago
1.3.6
6 years ago
1.3.5
6 years ago
1.3.2
6 years ago
1.3.0
6 years ago
1.2.7
6 years ago
1.2.6
6 years ago
1.2.5
6 years ago
1.2.4
6 years ago
1.2.3
6 years ago
1.2.2
6 years ago
1.2.1
6 years ago
1.2.0
6 years ago
1.1.0
6 years ago
1.0.2
6 years ago
1.0.1
6 years ago
1.0.0
6 years ago