Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/adalinesimonian/jshiki

Lightweight expression parsing and execution library for Node.js
https://github.com/adalinesimonian/jshiki

acorn expression-evaluator expression-parser expressions javascript nodejs npm typescript

Last synced: 3 days ago
JSON representation

Lightweight expression parsing and execution library for Node.js

Awesome Lists containing this project

README

        

![j式 — jshiki](assets/logo/jshiki-readme-banner.svg)

Safe and Easy Expression Evaluation for Node.js


Build Status
Codecov Coverage Status
npm Version


Documentation |
Change Log

---

jshiki provides a safe and easy way to evaluate expressions without worrying about external data being overwritten or accessed in unexpected ways. jshiki only has one lightweight dependency, [acorn], which it uses to parse expressions.

> **IMPORTANT!** jshiki is not a true sandbox. If you need to be able to evaluate arbitrary code of unknown origin, you may want to consider using [vm2] or a similar library.

### Basic Usage

```js
const jshiki = require('jshiki')

let result = jshiki.evaluate('(5 + 7) / 3') // result => 4
// or
let expression = jshiki.parse('(5 + 7) / 3')
result = expression() // result => 4
```

### Accessing data

```js
const code = "`Hello! My name's ${name.trim()}`"

expression = jshiki.parse(code)
result = expression({ name: ' Azumi ' })
// result => "Hello! My name's Azumi"

// or
result = jshiki.evaluate(code, {
scope: { name: ' Azumi ' },
})
// result => "Hello! My name's Azumi"
```

### Asynchronous evaluation

```js
const asyncCode = "`I'm ${await status()}...`"

expression = jshiki.parseAsync(asyncCode)
result = await expression({
status: async () => 'waiting',
})
// result => "I'm waiting..."

// or
result = await jshiki.evaluateAsync(asyncCode, {
scope: { status: async () => 'waiting' },
})
// result => "I'm waiting..."
```

For more examples, features, and information on how to use jshiki, see the [documentation].

## Discussion

Discuss jshiki on [GitHub discussions]. Make sure to follow the [code of conduct].

## Contributing

If you're looking for a way to contribute to jshiki, see the [contribution guide].

## Licence

[MIT](LICENCE)

[acorn]: https://github.com/acornjs/acorn
[vm2]: https://github.com/patriksimek/vm2
[documentation]: https://jshiki.io/latest/user-guide/
[github discussions]: https://github.com/adalinesimonian/jshiki/discussions
[code of conduct]: CODE_OF_CONDUCT.md
[contribution guide]: CONTRIBUTING.md