Giter Site home page Giter Site logo

chromegg / cel-js Goto Github PK

View Code? Open in Web Editor NEW
17.0 1.0 3.0 268 KB

cel-js is a Common Expression Language (CEL) evaluator for JavaScript

License: MIT License

TypeScript 97.47% JavaScript 2.53%
cel common-expression-language evaluator javascript type

cel-js's Introduction

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

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,

cel-js's People

Contributors

alvaro-saavedra-sumup avatar chromegg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

cel-js's Issues

0.1.0 release - MVP

This is my subject list of features that are bare minimum for v0.1.0 - MVP.

  • ConditionalOr
  • ConditionalAnd
  • Adds conditional type checking (to prevent JS behaviour like 2 || 4)
  • String concatenation
  • Nested Identifiers (foo.bar)
  • Unary operator (-123, !true)

Typecheck error - cannot find module helper.js

Hi, I am trying to run tsc to typecheck but I'm met with an error.

> tsc && tsc-alias

../node_modules/.pnpm/[email protected]/node_modules/cel-js/dist/errors/CelTypeError.d.ts:1:28 - error TS2307: Cannot find module '../helper.js' or its corresponding type declarations.

1 import { Operations } from '../helper.js';

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.