1.11.1 • Published 6 months ago
expression-engine v1.11.1
expression-engine
An expression tokenizer, parser and evaluator.
install
yarn add expression-engine
usage
import { tokenizeExpression, parseExpression, evaluateExpression, printExpression } from "expression-engine";
const tokens = tokenizeExpression('a + b')
const ast = parseExpression(tokens)
const result = evaluateExpression(ast, { a: 1, b: 2 })
const expression = printExpression(ast)
// <script src="./node_modules/expression-engine/expression-engine.min.js"></script>
supported expression features(ordered by priority from high to low)
- numeric literal: 123 0x123 0o123 0b101 2e2 123_456
- string literal: 'abc' "abc"
- boolean literal: true false
- array literal: a, b
- object literal: { a: 1 } { a: 1, ...b} { a }
null literal
group: ( )
- member expression and call expression: a.b a0 a'b' a(1, 2) a?.b a(...b) a?.0 a?.()
- unary expression: %
- unary expression: + - ! ~ not await
- binary expression: **
- binary expression: * / %
- binary expression: + -
- binary expression: << >> >>>
- binary expression: > < >= <=
- binary expression: == != === !==
- binary expression: && and
- binary expression: &
- binary expression: ^
- binary expression: |
- binary expression: || ?? or
- pipeline operator: |>
- condition expression: a ? b : c
custom data
import { tokenizeExpression, parseExpression, evaluateExpression, CustomData } from 'expression-engine'
class HighlightText implements CustomData {
constructor(public text: string, public highlight: number[]) { }
add(right: string | HighlightText) {
if (typeof right === 'string') {
return new HighlightText(this.text + right, this.highlight)
}
const highlight = [...this.highlight]
if (right.highlight.length > 0) {
const [first, ...remain] = right.highlight
highlight.push(first + this.text.length - highlight.reduce((p, c) => p + c, 0), ...remain)
}
return new HighlightText(this.text + right.text, highlight)
}
added(left: string) {
if (this.highlight.length > 0) {
const [first, ...remain] = this.highlight
return new HighlightText(left + this.text, [first + left.length, ...remain])
}
return new HighlightText(left + this.text, this.highlight)
}
equal(value: string | HighlightText) {
const text = typeof value === 'string' ? value : value.text
const highlight = typeof value === 'string' ? [] : value.highlight || []
if (text !== this.text) {
return false
}
if (this.highlight.length !== highlight.length) {
return false
}
for (let i = 0; i < highlight.length; i++) {
if (highlight[i] !== this.highlight[i]) {
return false
}
}
return true
}
}
const tokens = tokenizeExpression('a + b')
const ast = parseExpression(tokens)
const result = evaluateExpression(
ast,
{
a: new HighlightText('aaa', [1, 1]),
b: new HighlightText('bbb', [1, 1])
},
undefined,
[HighlightText]
)
// HighlightText { text: 'aaabbb', highlight: [ 1, 1, 2, 1 ] }
1.11.1
6 months ago
1.10.2
1 year ago
1.9.1
1 year ago
1.9.0
1 year ago
1.11.0
1 year ago
1.10.1
1 year ago
1.10.0
1 year ago
1.8.2
3 years ago
1.8.1
4 years ago
1.8.0
4 years ago
1.7.4
4 years ago
1.7.3
4 years ago
1.7.2
5 years ago
1.7.1
5 years ago
1.7.1-alpha.0
5 years ago
1.7.0
5 years ago
1.6.2
5 years ago
1.6.1
5 years ago
1.6.0
5 years ago
1.5.2
5 years ago
1.5.1
5 years ago
1.5.0
5 years ago
1.4.0
5 years ago
1.3.0
5 years ago
1.2.0
5 years ago
1.1.0
5 years ago
1.0.1
5 years ago