Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/trs/expression
:arrows_counterclockwise: Parse and evaluate Javascript-like expressions
https://github.com/trs/expression
ast expression hacktoberfest javascript syntax
Last synced: 10 days ago
JSON representation
:arrows_counterclockwise: Parse and evaluate Javascript-like expressions
- Host: GitHub
- URL: https://github.com/trs/expression
- Owner: trs
- Created: 2022-05-05T05:31:04.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-28T21:35:49.000Z (about 1 year ago)
- Last Synced: 2023-10-29T19:30:11.477Z (about 1 year ago)
- Topics: ast, expression, hacktoberfest, javascript, syntax
- Language: TypeScript
- Homepage:
- Size: 228 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `@trs/expression`
Evaluate arbitrary expressions using a Javascript-like syntax.
Supported expressions:
- Arithmetic - evaluates to a number
- String - evaluates to a string
- Logical - evaluates to a boolean## Install
```sh
npm install @trs/expression
# or
yarn add @trs/expression
# or
pnpm add @trs/expression
```## Evaluate Expression String
Expression strings can be evaluated to a single value.
```ts
import { evaluate } from '@trs/expression';const ast = evaluate('1 + 2 == 3');
assert(ast === true);
```## Parse Expression String
Expression strings can be parsed into an expression tree.
```ts
import { parse } from '@trs/expression';const ast = parse('1 + 2 == 3');
assert(ast == {
type: "BinaryExpression",
... etc
});
```## Evaluate Expression Tree
Expression trees can be evaluated to a single value.
```ts
import { evaluate } from '@trs/expression';const result = evaluate({
type: "BinaryExpression",
operator: "==",
left: {
type: "BinaryExpression",
operator: "+",
left: {
type: "Literal",
kind: "number",
value: "1"
},
right: {
type: "Literal",
kind: "number",
value: "2"
}
},
right: {
type: "Literal",
kind: "number",
value: "3"
}
});assert(result === true);
```