Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ympons/expreso
:coffee: A boolean expression parser and evaluator in Elixir.
https://github.com/ympons/expreso
boolean-expression elixir eval expreso lexer parser
Last synced: 3 months ago
JSON representation
:coffee: A boolean expression parser and evaluator in Elixir.
- Host: GitHub
- URL: https://github.com/ympons/expreso
- Owner: ympons
- License: mit
- Created: 2017-02-02T08:09:19.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-01-22T04:48:16.000Z (about 5 years ago)
- Last Synced: 2024-10-31T18:26:59.037Z (3 months ago)
- Topics: boolean-expression, elixir, eval, expreso, lexer, parser
- Language: Elixir
- Homepage:
- Size: 22.5 KB
- Stars: 66
- Watchers: 8
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Expreso [![Build Status](https://travis-ci.org/ympons/expreso.svg?branch=master)](https://travis-ci.org/ympons/expreso) [![Hex Version](https://img.shields.io/hexpm/v/expreso.svg)](https://hex.pm/packages/expreso)
An Elixir library for parsing and evaluating boolean expressions## Installation
From [Hex](https://hex.pm/packages/expreso), the package can be installed by adding it to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:expreso, "~> 0.1.1"}]
end
```## Usage
```
Expreso.eval(expression, variables)
```Parses and evaluates the provided expression. The variables can be supplied as a map in the `variables` parameter.
eg.
```
# Evaluate if a + 2 is between 3 and 1
iex> Expreso.eval("a + 2 in (3, 1)", %{"a" => 1})
iex> {:ok, true}# Check if the key "test" is "works"
iex> Expreso.eval("test = 'works'", %{"test" => "works"})
iex> {:ok, true}# Check if the key "test" is "works" or "great"
iex> Expreso.eval("test = 'works' or test = 'great'", %{"test" => "great"})
iex> {:ok, true}# Evaluate an expression with not_in_op and sum
iex> Expreso.eval("10 + 2 not in (3, 1)")
iex> {:ok, true}# Evaluate an expression with and_logic
iex> Expreso.eval("a + 2 in (2, 3) and 1 + 1 >= 2", %{"a" => 1})
iex> {:ok, true}# Evaluate an expression with string
iex> Expreso.eval("a = 'Hello' and b != 'World' and 1 + 1 = 2", %{"a" => "Hello", "b" => ""})
iex> {:ok, true}# Evaluate an expression with an array variable
iex> Expreso.eval("a in b", %{"a" => 1, "b" => [2, 1]})
iex> {:ok, true}# Evaluate another expression with an array variable
iex> Expreso.eval("a not in b", %{"a" => 1, "b" => [2, 1]})
iex> {:ok, false}# Evaluate an expression using not operator
iex> Expreso.eval("not 1 > 1 + a", %{"a" => 2})
iex> {:ok, true}
```