Packages
expression
0.3.3
3.0.0-rc.1
3.0.0-rc.0
2.49.1
2.49.0
2.48.0
2.47.5
2.47.4
2.47.3
2.47.2
2.47.1
2.47.0
2.46.0
2.45.1
2.45.0
2.44.1
2.44.0
2.43.0
2.42.1
2.41.4
2.41.3
2.41.2
2.41.0
2.40.0
2.38.0
2.37.0
2.36.2
2.36.1
2.36.0
2.35.0
2.34.0
2.33.1
2.33.0
2.32.1
2.31.6
2.31.3
2.31.0
2.30.0
2.28.0
2.27.3
2.27.2
2.27.1
2.27.0
2.26.1
2.26.0
2.25.0
2.24.2
2.24.1
2.24.0
2.23.9
2.23.8
2.23.7
2.23.6
2.23.4
2.23.3
2.23.2
2.23.1
2.23.0
2.22.2
2.22.0
2.21.0
2.20.0
2.19.0
2.18.0
2.17.0
2.16.0
2.14.0
2.13.0
2.12.0
2.11.0
2.10.1
2.10.0
2.9.0
2.8.0
2.6.0
2.5.8
2.5.7
2.5.6
2.5.5
2.5.4
2.5.3
2.5.2
2.5.1
2.5.0
2.4.1
2.4.0
2.3.2
2.3.1
2.3.0
2.2.1
2.2.0
2.1.1
2.1.0
2.0.5
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
1.1.1
1.1.0
1.0.0
0.7.2
0.7.1
0.7.0
0.6.0
0.5.0
0.4.3
0.4.2
0.4.1
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.1.0
A Excel like expression parser, compatible with FLOIP Expression language.
Current section
Files
Jump to
Current section
Files
expression
README.md
README.md
# Expression
An Elixir library implementing the [FLOIP Expressions](https://floip.gitbook.io/flow-specification/expressions) language.
```elixir
iex> Expression.evaluate("Hello @name", %{
"name" => "World"
})
"Hello World"
iex> Expression.evaluate("Hello @contact.name", %{
"contact" => %{
"name" => "peter"
}
})
"Hello peter"
iex> Expression.evaluate("Hello @contact.name, you were born in @(YEAR(contact.birthday))", %{
"contact" => %{
"name" => "mary",
"birthday" => "1920-02-02T00:00:00"
}
})
{:ok, "Hello mary, you were born in 1920"}
iex> Expression.evaluate("Hello @PROPER(contact.name)", %{
"contact" => %{
"name" => "peter rabbit"
}
})
"Hello Peter Rabbit"
iex> Expression.evaluate("Your next appointment is @(EDATE(contact.appointment, 1))", %{
"contact" => %{
"appointment" => DateTime.utc_now()
}
})
{:ok, "Your next appointment is 2021-01-02 12:38:14.426663Z"}
iex> Expression.evaluate("Your next appointment is @(DATEVALUE(EDATE(contact.appointment, 1), \"%Y-%m-%d\"))", %{
"contact" => %{
"appointment" => "2020-12-13T23:35:55"
}
})
{:ok, "Your next appointment is 2021-01-13"}
iex> Expression.evaluate("Dear @IF(contact.gender = 'M', 'Sir', 'Client')", %{
"contact" => %{
"gender" => "O"
}
})
{:ok, "Dear Client"}
```
See `Engaged.Callbacks` for all the functions implemented.
# Types
Expression knows the following types:
```elixir
iex> # Decimals
iex> Expression.evaluate("@(1.23)")
{:ok, #Decimal<1.23>}
iex> # Integers
iex> Expression.evaluate("@(1)")
{:ok, 1}
iex> # DateTime in ISO and a sloppy US formats
iex> Expression.evaluate("@(2020-12-13T23:35:55)")
{:ok, ~U[2020-12-13 23:35:55.0Z]}
iex> Expression.evaluate("@(13-12-2020 23:35:55)")
{:ok, ~U[2020-12-13 23:35:55Z]}
iex> # case insensitive booleans
iex> Expression.evaluate("@(true)")
{:ok, true}
iex> Expression.evaluate("@(TrUe)")
{:ok, true}
iex> Expression.evaluate("@(false)")
{:ok, false}
iex> Expression.evaluate("@(FaLsE)")
{:ok, false}
```
# Future extensions
- It may be worth implementing a binary only expression parser, something
that is guaranteed to only return a `true` or `false`. That could be useful
when building decision trees with dynamic conditionals depending on context.
## Installation
> This is not available in Hex.pm or Hexdocs.pm yet
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `expression` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:expression, "~> 0.1.0"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/expression](https://hexdocs.pm/expression).