@fleker/cel-js
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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

Create a list

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

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`

Readme

Keywords

none

Package Sidebar

Install

npm i @fleker/cel-js

Weekly Downloads

2

Version

1.0.0

License

none

Unpacked Size

161 kB

Total Files

32

Last publish

Collaborators

  • fleker