@0y0/exec
v1.0.2
Published
Execute code and get the return.
Downloads
13
Maintainers
Readme
@0y0/exec ·
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