@0y0/exec 中文文档教程
@0y0/exec ·
exec
是一个执行代码并获取返回值的沙箱。
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
@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