Socket
Book a DemoInstallSign in
Socket

cel-js

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cel-js

Common Expression Language (CEL) evaluator for JavaScript

latest
Source
npmnpm
Version
0.8.2
Version published
Weekly downloads
6.7K
-21.68%
Maintainers
1
Weekly downloads
 
Created
Source

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
        • single-quote string
        • double-quote string
        • raw string
        • triple-quote string
        • byte string
      • hexadecimal
      • 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: (has, size, etc.)
      • All (e.all(x, p))
      • Exists (e.exists(x, p))
      • Exists one (e.exists_one(x, p))
      • Filter (e.filter(x, p))
      • Has (has(foo.bar))
      • Map (e.map(x, t) and e.map(x, p, t))
      • Size (size(foo))
    • Unary Operators (!true, -123)
    • Custom Functions (myFunction())
    • Comments (// This is a comment)

Installation

To install cel-js, use npm:

npm i cel-js

Usage

evaluate

evaluate is the primary function for parsing and evaluating CEL expressions. It takes an expression string and an optional object of variables to use in the expression.

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

parse

parse is a lower-level function that only parses an expression string into an AST. This can be useful if you want to evaluate the expression multiple times with different variables or if you want to validate the syntax of an expression.

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

if (!result.isSuccess) {
  // your business logic
}

// you can reuse the result of `parse` to evaluate the expression
evaluate(result.cst, { a: 2 }) // => 4
evaluate(result.cst, { a: 4 }) // => 6

Known Issues

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

Keywords

Common Expression Language

FAQs

Package last updated on 11 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts