code-builder

1.0.2 • Public • Published

code-builder

Build Status

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 = require('code-builder');

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 fn = function(){
  this.addString('[1,2,3]')
}
 
CodeBuilder( fn ).run() == [1,2,3]
var code = CodeBuilder();
 
code.Builder.addString('[1,2,3]')
 
code.run() == [1,2,3]
var fn = function(){
  this.addString('function go(){ return ')
  this.addString('[1,2,3]')
  this.addString('}')
  this.addString('go()')
}
 
CodeBuilder( fn ).run() == [1,2,3]

Executing a Script with a Context

var fn = function(){
    this.addString('function go(){ return [a,b,c] }')
    this.addString('go()')
  },
  context = {
    a : 1,
    b : 2,
    c : 3
  }
 
CodeBuilder( fn ).run( context ) == [1,2,3]

Executing an External Function

var result,
  setResult = function( value ){
    result = value;
  },
  fn = function(){
    this.addString('function go(){ return [1,2,3] }')
    this.addFunction( setResult, 'go()' );
  }
 
CodeBuilder( fn ).run()
 
result == [1,2,3]

Externally Manipulating Script Objects

var updateArray = function( arr ){
    arr.push(4)
  },
  fn = function(){
    this.addString('function go(){ return [1,2,3] }')
    this.addString('arr = go()')
    this.addFunction( updateArray, 'arr' );
    this.addString('arr')
  }
 
CodeBuilder( fn ).run() == [1,2,3,4]

Executing External Scripts

var fn1 = function(){
    this.addString('var result;');
    this.addString('function double( str ){ return str + str }')
    this.addScript( fn2, 'double( name )' );
    this.addString('result;');
  },
  fn2 = function( str ){
    this.addString('result = "That name is ');
    
    if( str.length > 15 ){
      this.addString('Long"')
    }
    else{
      this.addString('Short"')
    }
  },
  code = CodeBuilder( fn );
  
code.run({ name : 'Veronica' }) == 'That name is Long'
code.run({ name : 'Fred' }) == 'That name is Short'

License

MIT

Package Sidebar

Install

npm i code-builder

Weekly Downloads

1

Version

1.0.2

License

MIT

Last publish

Collaborators

  • bninni