https://github.com/leeoniya/uexpr
A conditional expression compiler
https://github.com/leeoniya/uexpr
Last synced: 7 months ago
JSON representation
A conditional expression compiler
- Host: GitHub
- URL: https://github.com/leeoniya/uexpr
- Owner: leeoniya
- License: mit
- Created: 2023-11-17T19:01:45.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-02T12:20:35.000Z (almost 2 years ago)
- Last Synced: 2025-03-25T05:24:46.885Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 14.6 KB
- Stars: 12
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## μExpr
A conditional expression compiler _(MIT Licensed)_
---
### Introduction
μExpr is a compiler for conditional expressions and filters/matchers that can be stored in JSON.
It is similar in purpose to https://jsonlogic.com/ with the following differences:
1. Simplified syntax, without objects
2. Reduced scope that focuses on matching (emitting either `true` or `false`)
3. Attains native (25x faster) runtime performance by using `new Function()` with carefully placed guards
---
### Example
```js
import {
compileExpr,
compileMatcher,
compileFilter
} from "../src/uExpr.mjs";
let matcher = compileMatcher(
['||',
['^', '.name', 'foo'],
['&&',
['==', '.meta.dataTopic', 'unsubscribe'],
['some', '.fields', ['==', '.type', 'string']]
],
]
);
```
If you were to write this logic by hand it would look like this:
```js
let matcher = ($, $i = 0) => (
$.name.startsWith("foo") ||
(
$.meta.dataTopic == "unsubscribe" &&
$.fields.some(($, $i) => $.type == "string")
)
);
```
Now you can use the compiled function as the callback to `Array.filter()`:
```js
let frames = [
{
name: 'foobar',
meta: {
dataTopic: 'subscribe'
},
fields: [
{
type: 'number'
},
]
},
{
name: 'something',
meta: {
dataTopic: 'unsubscribe'
},
fields: [
{
type: 'string'
},
]
},
{
name: 'other',
meta: {
dataTopic: 'cancellation'
},
fields: [
{
type: 'string'
},
]
},
];
let filtered = frames.filter(matcher);
```