npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

code-builder

v1.0.2

Published

Anonymously execute external functions from another script

Downloads

23

Readme

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