Skip to content

Common Expression Language (CEL) evaluator for JavaScript

License

Notifications You must be signed in to change notification settings

alvaro-saavedra-sumup/cel-js

 
 

Repository files navigation

cel-js

cel-js is a powerful and efficient parser and evaluator for Google's Common Expression Language (CEL), built on the robust foundation of the Chevrotain parsing library. This library aims to provide a seamless and easy-to-use interface for working with CEL in JavaScript environments.

Live Demo 🚀

Try out cel-js in your browser with the live demo.

Features ✨

  • 🚀 Fast and Efficient Parsing: Leverages Chevrotain for high-performance parsing and evaluation
  • 🌍 Isomorphic: Ready for server and browser
  • 📦 ESM support
  • 📚 Supported CEL Features:
    • Literals
      • int
      • uint
      • double
      • bool
      • string
      • bytes
      • list
      • map
      • null
    • Conditional Operators
      • Ternary (condition ? true : false)
      • Logical And (&&)
      • Logical Or (||)
    • Equality Operators (==, !=)
    • Relational Operators (<, <=, >, >=, in)
    • Arithmetic Operators (+, -, *, /, %)
    • Identifiers
      • Dot Notation (foo.bar)
      • Index Notation (foo["bar"])
    • Macros: (exists, has, size, etc.)
      • Exists (exists(foo))
      • Has (has(foo, "bar"))
      • Size (size(foo))
    • Unary Operators (!true, -123)

Installation

To install cel-js, use npm:

npm i cel-js

Usage

import { evaluate, parse } from 'cel-js'

// use `evaluate` to parse and evaluate an expression
evaluate('2 + 2 * 2') // => 6

evaluate('"foo" + "bar"') // => 'foobar'

evaluate('user.role == "admin"', { user: { role: 'admin' } }) // => true

// use `parse` to parse an expression, useful for validation purposes
const result = parse('2 + 2')

if (!result.isSuccess) {
  throw new Error('Invalid syntax')
}

// you can reuse the result of `parse` to evaluate the expression
evaluate(result.cst) // => 4

Known Issues

  • Errors types and messages are not 100% consistent with the cel-go implementation

About

Common Expression Language (CEL) evaluator for JavaScript

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 97.1%
  • JavaScript 2.9%