README
exec
is a sandbox to execute code and get the return.
Installation
npm install --save @0y0/exec
Usage
const exec = require('@0y0/exec')
{{
and }}
.
1. Run the string type of code wrapped with 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']
for/of
4. Run the array type of code with exec(
['for(item of list)', { value: '{{item.name}}' }],
{ list: [{ name: 'JC' }] }
)
// [{ value: 'JC' }]
for
5. Run the array type of code with 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