Files

95 lines
2.5 KiB
Markdown
Raw Permalink Normal View History

2023-02-28 15:18:55 -08:00
# actions/expressions
2023-01-23 15:31:47 -08:00
2023-02-28 15:18:55 -08:00
`@actions/expressions` is a library to parse and evaluate GitHub Actions [expressions](https://docs.github.com/actions/learn-github-actions/expressions).
2023-01-23 15:31:47 -08:00
## Installation
2023-01-24 10:14:39 -08:00
The [package](https://www.npmjs.com/package/@actions/expressions) contains TypeScript types and compiled ECMAScript modules.
2023-01-23 15:31:47 -08:00
```bash
2023-01-25 09:12:44 -08:00
npm install @actions/expressions
2023-01-23 15:31:47 -08:00
```
## Usage
### Simple example
```ts
2023-01-24 10:14:39 -08:00
import { Parser, Lexer, Evaluator, data } from '@actions/expressions';
2023-01-23 15:31:47 -08:00
const lexer = new Lexer("1 == 2");
const lr = lexer.lex();
const parser = new Parser(lr.tokens, [], []);
const expr = parser.parse();
const evaluator = new Evaluator(expr, new data.Dictionary());
const result = evaluator.evaluate();
console.log(result.coerceString()) // false
```
### With context data
```ts
2023-01-24 10:14:39 -08:00
import { Parser, Lexer, Evaluator, data } from '@actions/expressions';
2023-01-23 15:31:47 -08:00
const lexer = new Lexer("'monalisa' == context.name");
const lr = lexer.lex();
const parser = new Parser(lr.tokens, ["context"], []);
const expr = parser.parse();
const evaluator = new Evaluator(expr, new data.Dictionary([{
key: "context"
value: new data.Dictionary([{
key: "name"
value: new data.StringData("monalisa")
}])
}]));
const result = evaluator.evaluate();
console.log(result.coerceString()) // true
```
## Contributing
2023-01-24 17:40:24 -08:00
See [CONTRIBUTING.md](../CONTRIBUTING.md) at the root of the repository for general guidelines and recommendations.
2023-01-23 15:31:47 -08:00
2023-01-23 17:49:52 -08:00
This project is just one of multiple implementations of the GitHub Actions Expressions language. We therefore cannot accept contributions that add new language features or significantly change the behavior of existing language features. If you would like to propose a change to the language itself, please use our [Community Forum](https://github.com/community/community/discussions/categories/actions-and-packages).
2023-01-23 15:31:47 -08:00
If you do want to contribute, please run [prettier](https://prettier.io/) to format your code and add unit tests as appropriate before submitting your PR. [./testdata](./testdata) contains test cases that all implementations should pass, please also make sure those tests are still passing.
2023-01-23 15:38:07 -08:00
### Build
```bash
2023-01-25 09:12:44 -08:00
npm run build
2023-01-23 15:38:07 -08:00
```
or to watch for changes
```bash
2023-01-25 09:12:44 -08:00
npm run watch
2023-01-23 15:38:07 -08:00
```
### Test
```bash
2023-01-25 09:12:44 -08:00
npm test
2023-01-23 15:38:07 -08:00
```
or to watch for changes and run tests:
```bash
2023-01-25 09:12:44 -08:00
npm run test-watch
2023-01-23 15:38:07 -08:00
```
### Lint
```bash
2023-01-25 09:12:44 -08:00
npm run format-check
2023-01-23 15:38:07 -08:00
```
2023-01-23 15:31:47 -08:00
## License
This project is licensed under the terms of the MIT open source license. Please refer to [MIT](../LICENSE) for the full terms.