code-builder
Anonymously execute external functions from another script
It allows you to build a script which runs in a separate context while also executing external code at specified locations.
The external code is inaccessible from anywhere else within the script
Install
npm install code-builder
or
npm install -g code-builder
Then import the module into your program:
var CodeBuilder = ;
Usage
CodeBuilder( fn )
- fn (Function) - Optional - The function used to create the Script
The input function is run a context of object containing three methods:
To append raw Javascript code to the Script:
- this.addString( str )
- str (String) - The string representation of raw Javascript code to append to the Script
To append an external function call to the Script:
- this.addFunction( fn, str )
- fn (Function) - The external function to call at the current location in the Script
- str (String) - Optional - The string representation of raw Javascript code to place inside the call parenthesis
To append an external Script execution to the Script:
- this.addScript( fn, str )
- fn (Function) - The function used to create the new Script, whhich will be placed at the current Location in the Script
- str (String) - Optional - The string representation of raw Javascript code to place inside the call parenthesis
It returns an Object with the following properties:
- Builder - Object containing the above three methods
- Builder.addString( str )
- Builder.addFunction( fn, str )
- Builder.addScript( fn, str )
To run the Script in a certain context:
- run( context )
- context (Object) - Optional - An objecting containing any values you want to assign as global variables within the Script
Returns the result of the last statement in the Script
Examples
Executing a Script
var { this} == 123
var code = ; codeBuilder code == 123
var { this this this this} == 123
Executing a Script with a Context
var { this this } context = a : 1 b : 2 c : 3 == 123
Executing an External Function
var result { result = value; } { this this; } result == 123
Externally Manipulating Script Objects
var { arr } { this this this; this } == 1234
Executing External Scripts
var { this; this this; this; } { this; if strlength > 15 this else this } code = ; code == 'That name is Long'code == 'That name is Short'