Socket
Socket
Sign inDemoInstall

argument-injector

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    argument-injector

arguments injector for node


Version published
Maintainers
1
Install size
33.0 kB
Created

Readme

Source

Build Status Coverage Status

argument-injector is a function argument injector for JavaScript. it allows any function to be "bound" to an injector object, which hijacks the function call and injects arguments when the function is executed.

usage

to get started, you'll first need to create a new instance of an Injector:

var Injector = require('argument-injector'),
    injector = new Injector();

this injector object will have three methods you care about: #register, #bind, and #trigger

register

this method allows you to "save" a variable in the injector object. there are the variables that are available during a function call. this is the signature of the method: Injector register(String name, * variable)

injector.register('myService', {
    isActive: function () {
        return true;
    }
});
bind

the bind method is what ties a function to your injector. this works by taking your function, and returning a copy which checks for arguments passed and arguments available in the injector when the function is invoked. this is the signature of the method: Function bind(Function func, Object scope)

var myFunction = injector.bind(function (myService) {
    return myService.isActive() ? 'you are active' : 'you are not active';
});

you can call myFunction the same way you would any other function, with the exception that you do not need to pass it a myService variable.

bind will parse the signature of the function you gave it and generate a list of the required arguments. once the function is invoked, this list of arguments is checked against the registered variables and passed to the real function.

trigger

there's also a trigger method, which works just like bind, except it will invoke the funciton immediatelly and return that call's return value instead of a bound function. this is the signature of the method: * trigger(Function func, Object scope)

injected.register('name', 'Marcos');
injector.trigger(function (name) {
    // will output 'Marcos'
    console.log(name);
});

Keywords

FAQs

Last updated on 29 Mar 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc