@0y0/exec

Execute code and get the return.

Usage no npm install needed!

<script type="module">
  import 0y0Exec from 'https://cdn.skypack.dev/@0y0/exec';
</script>

README

@0y0/exec ยท GitHub license npm

exec is a sandbox to execute code and get the return.

Installation

npm install --save @0y0/exec

Usage

const exec = require('@0y0/exec')

1. Run the string type of code wrapped with {{ and }}.

exec('{{`Hi, ${name}`}}', { name: 'JC' })
// 'Hi, JC'

2. Run the object type of code

exec({ value: '{{name}}' }, { name: 'JC' })
// { value: 'JC' }

3. Run the array type of code

exec(['{{name}}'], { name: 'JC' })
// ['JC']

4. Run the array type of code with for/of

exec(
  ['for(item of list)', { value: '{{item.name}}' }],
  { list: [{ name: 'JC' }] }
)
// [{ value: 'JC' }]

5. Run the array type of code with for

exec(
  ['for(i=0;i<list.length;i++)', { value: '{{list[i].name}}' }],
  { list: [{ name: 'JC' }] }
)
// [{ value: 'JC' }]

6. Use global variables

exec.define('add', (a, b) => a + b)
exec('{{add(1,2)}}')
// 3

License

MIT