Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fleker/cel-js
A port of Common Expression Language that can run in a Node or browser JS environment
https://github.com/fleker/cel-js
filter typescript web
Last synced: 11 days ago
JSON representation
A port of Common Expression Language that can run in a Node or browser JS environment
- Host: GitHub
- URL: https://github.com/fleker/cel-js
- Owner: Fleker
- Created: 2022-02-07T17:56:35.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-03-25T01:52:41.000Z (11 months ago)
- Last Synced: 2024-04-14T18:29:38.721Z (10 months ago)
- Topics: filter, typescript, web
- Language: TypeScript
- Homepage: http://felker.dev/cel-js/
- Size: 229 KB
- Stars: 14
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Common Expression Language - JS
## Supported functionality (subset)
[x] Basic types and primitives
[x] Comparisons
[x] Lists
[~] Logic - Ternary conditional, logical AND, logical OR
[ ] etc## Usage
```
npm install @fleker/cel-js
```### Create a list
```javascript
const expr = '["-1"]'
const expected = {
list_value: {
values: { string_value: `-1` }
}
}const celSpec = new CelSpec();
const ast = celSpec.toAST(expr, {});
const tf = new TextFormatter({}, {})const cel = tf.format(ast)
expect(cel).toStrictEqual(expected) // Returns `true`
```### Pass-in variables through a JSON object
```javascript
const expr = 'x'
const bindings = {
x: 123
}
const expected = {
int64_value: 123
}
const celSpec = new CelSpec();
const ast = celSpec.toAST(expr, {});
const bindingsAst = (() => {
if (!bindings) return {}
const tf = new TextFormatter({}, bindings)
let res = {}
for (const [key, entry] of Object.entries(bindings)) {
const entryAst = celSpec.toAST(`${entry}`)
const entryCel = tf.format(entryAst)
res[key] = entryCel
}
return res
})()
const tf = new TextFormatter({}, bindingsAst)const cel = tf.format(ast)
expect(cel).toStrictEqual(expected) // Returns `true`
```