dynamic-function

1.1.0 • Public • Published

node-dynamic-function

Easily create functions with dynamic name and length. This is useful for wrapping functions without f'ing up their name and length.

Build Status Coverage Status Known Vulnerabilities dependencies Status Downloads


Install

npm install --save dynamic-function

Usage

const dynamicFunction = require('dynamic-function');
 
function originalFunction(originalArg1, originalArg2) {
    /* ... */
}
 
const wrappedFunction = dynamicFunction(originalFunction, {
  name: 'wrapped_' + originalFunction.name,
  length: originalFunction.length,
});
 
console.log(wrappedFunction.name);   // logs 'wrapped_originalFunction'
console.log(wrappedFunction.length); // logs 2
 
console.log(wrappedFunction.toString());
 
// function wrapped_originalFunction(originalArg1, originalArg2) {
//   /* ... */
// }

API

dynamicFunction ( func [, options] ) func : function options : object (optional) options.bind : object (default: null) options.name : string (default: '') options.length : number (default: 0)

Performance

Some environments (eg. NodeJS < 4.x.x) don't support renaming functions with Object.defineProperty. In this case there is a fallback to eval.

eval a relatively slow JavaScript feature. Avoid (over)using this in situations where high performance is required.

At start-up (for Server applications) / low load situations using this should pose no problem.

/dynamic-function/

    Package Sidebar

    Install

    npm i dynamic-function

    Weekly Downloads

    6

    Version

    1.1.0

    License

    MIT

    Last publish

    Collaborators

    • moeriki