Not production ready yet
Common Expression Language for JavaScript
The Common Expression Language (CEL) is a non-Turing complete language designed for simplicity, speed, safety, and portability. The goal of this library is to adhere to the language definition of CEL and enable its use within a JavaScript project.
This library uses Peggy to match the CEL language definition. To utilize the pegjs file for making modifications, you can copy the following File and access the Online Editor. There, you can directly test the parser's outcomes.
Installation
To install cel-javascript
:
npm install cel-javascript
Usage
Presently, it's possible to evaluate a string without custom types. To do so, add the following line:
import { parseString } from 'cel-javascript';
And then you can do the following:
parseString("0"); // => {int64_value: 0}
parseString("0u"); // => {uint64_value: 0}
parseString("r'banana'"); // => {string_value: "banana"}
parseString("10 + 10 + 10"); // => {double_value: 30}
parseString("3 > 2 ? 1 : 4"); // => {int64_value: 1}
Additionally, you can use custom types by adding:
import { parseStringWithOptions } from 'cel-javascript';
To utilize custom types, pass a JSON object as a String to the function for reference:
let json = `{"banana": {"score": 1}}`
parseStringWithOptions("banana.score", json); // => {int64_value: 1}
The reason why it currently needs the JSON as a string is that I was currently not able because of the limitation of
javascript and how it handles numbers to make sure that 1.0
would be read as a double
instead of an int
. To currently fix
this I added the lossless-json library which enables me to at the time of fetching the
value make the correct call to return a Double
, Int64
or String
value. This is something I want to try to fix in the future.