expression-language
Version:
Javascript implementation of symfony/expression-language
97 lines (79 loc) • 2.73 kB
Markdown
The idea is to be able to evaluate the same expressions client-side (in Javascript with this library)
and server-side (in PHP with the Symfony/ExpressionLanguage).
```bash
npm install expression-language
yarn add expression-language
```
You can also use this library directly in the browser by including it via a script tag:
```html
<!-- Unminified version for development -->
<script src="https://unpkg.com/expression-language/dist/expression-language.js"></script>
<!-- or minified version for production -->
<script src="https://unpkg.com/expression-language/dist/expression-language.min.js"></script>
```
```javascript
import { ExpressionLanguage } from "expression-language";
let expressionLanguage = new ExpressionLanguage();
```
```html
<script src="https://unpkg.com/expression-language/dist/expression-language.min.js"></script>
<script>
// The library is available as a global ExpressionLanguage object
const expressionLanguage = new ExpressionLanguage.ExpressionLanguage();
</script>
```
A complete browser example is available in the [examples/browser-usage.html](examples/browser-usage.html) file.
```javascript
let result = expressionLanguage.evaluate('1 + 1');
// result is 2.
```
```javascript
let result = expressionLanguage.evaluate(
'a > 0 && b != a',
{
a: 1,
b: 2
}
);
// result is true
```
```javascript
let expression = 'a[2] === "three" and b.myMethod(a[1]) === "bar two"';
let values = {
a: ["one", "two", "three"],
b: {
myProperty: "foo",
myMethod: function(word) {
return "bar " + word;
}
}
};
let result = expressionLanguage.evaluate(expression, values);
// result is true
```
This package is automatically published to npm when changes are pushed to the main branch using GitHub Actions.
If you're maintaining this package, you'll need to set up an NPM authentication token:
1. Generate an npm access token with publish permissions:
- Go to npmjs.com and log in
- Click on your profile picture → Access Tokens
- Click "Generate New Token" → Select "Publish"
2. Add the token to your GitHub repository:
- Go to your GitHub repository → Settings → Secrets and variables → Actions
- Click "New repository secret"
- Name: `NPM_TOKEN`
- Value: Your npm access token
- Click "Add secret"
Once set up, any push to the main branch will trigger the workflow to test and publish the package.