script-eval-js
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

Script Runner

Make your eval pass parameters and specify this context when executing the script, and you can register global functions and variables for him.

License: MIT

install

npm install script-eval-js


import { ScriptRunner } from 'script-eval-js';

examples

const result = ScriptRunner.eval(`return 1+1;`);
console.log(result);
const result = ScriptRunner.eval(`console.log(this);`);
console.log(result);
const result = ScriptRunner.eval(`console.log(id+name);`, { id: 100,  name: 'script runner' });
console.log(result);
const func = ScriptRunner.buildFunction(`console.log(this); return id + name;`, ['id','name'],this);
const result = func(100,'script runner');
console.log(result);

typescript typing

/**
* global context
*/
export declare class ScriptEvalContext {
    constructor(baseContext?: ScriptEvalContext, initLib?: boolean);
    clear(): void;
    register(name: string, object: any): void;
    remove(name: string): void;
    get(name: string): any;
    get names(): string[];
    get values(): any[];
}
export declare class ScriptRunner {
    static buildFunction(script: string, argNames?: string[], thisContext?: Object, globalContext?: ScriptEvalContext): Function;
    static eval<TResult>(script: string, parameters: Record<string, any>, thisContext?: Object, globalContext?: ScriptEvalContext): TResult;
}

Package Sidebar

Install

npm i script-eval-js

Weekly Downloads

1

Version

1.0.7

License

MIT

Unpacked Size

16.5 kB

Total Files

7

Last publish

Collaborators

  • vblegend