# script-handler

> [![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)

Latest version **0.0.1** (published 2017-03-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install script-handler
pnpm add script-handler
yarn add script-handler
bun add script-handler
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2017-03-31 |
| First published | 2017-03-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Michiel van der Velde |
| Maintainers | michielvdvelde |
| Keywords | vm, script, context, validation |

## Links

- npm: https://www.npmjs.com/package/script-handler
- Repository: https://github.com/MichielvdVelde/script-handler
- Issues: https://github.com/MichielvdVelde/script-handler/issues
- npm.io page: https://npm.io/package/script-handler

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.1 (latest) — 2017-03-31

## README

# script-handler

[![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)

Run a custom JS function inside `vm`, providing a flexible way to validate complex
input. Simple to use and does only one thing, so no bells and whistles, which might be just what you need.

## Install

`npm i script-handler`

## Usage

`script-handler` allows you to define a function within a `vm`, which can then be
used to validate complex input.

A script can come from any source (security is outside the scope of this module)
and is compiled only once, saving precious time.

The `script` defines a global variable `process`, which should be a `function`.
`process` can then be called with arbitrary data by using `compiled.process()` (see example below).

A simple annotated example should suffice:

```js
const ScriptHandler = require('script-handler')

// Our script defines a `process` variable which is the function
// that will be called later.
const script = `process = function (row) {
  return row.type === 'credit' && row.code === 'ba'
}`

// We only have to compile the script once, saving clock cycles
const compiled = new ScriptHandler(script)

// In order for the script to set the `process` variable, we have to run it
// When the script has finished running, the global variable `process`
// should be a function (typeof process === 'function')
compiled.run().then(() => {
  // Now our process handler is set
  console.log('Script successfully ran')

  // Some data to validate.
  // vm.process() only supports one argument at the moment
  const row = {
    type: 'credit',
    code: 'ba'
  }

  // Execute the process handler with the `row` data
  // In this case, `valid` will be true
  const valid = vm.process(row)
  console.log(`Row valid: ${valid}`)
}).catch(err => {
  console.log(err)
})
```

### To Do

* Allow any arbitrary amount of arguments to be passed to `process`
* Try to find a way to make calling `vm.process()` asynchronous
* Update example to show how to make use of `options` and `sandbox`

### Changelog

* **v0.0.1** (31-03-2017)
  * First published

[![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)

### License

Copyright 2017 [Michiel van der Velde](http://www.michielvdvelde.nl).

This software is licensed under the [MIT License](LICENSE).

---
_Source: https://npm.io/package/script-handler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
