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 🙏

© 2025 – Pkg Stats / Ryan Hefner

param-injection

v0.1.0

Published

(Node.js) Wraps a function so that one or some of the parameters can be auto-loaded at run time. Mainly used to build modules or functions that may have some run time dependencies.

Readme

Param Injection

(Node.js) Wraps a function so that one or some of the parameters can be auto-loaded at run time. Mainly used to build modules or functions that may have some run time dependencies.

This is similar to "Dependency Injection" but much simpler.

What

It exports a function.

var injectify = require('param-injection');

And that can be used to wrap a function, so that one or some of the parameters of the function can be actively retrieved (with a given getter) at run time.

For example, say you have a function that does something, and requires some arguments.

var request = require('request');
var Promise = require('bluebird');

function requestSomething(url, other, args) {
    return new Promise(function(resolve, reject) {
        request(url, function(err, res) {
            if (err) return reject(err);
            resolve(res);
        });
    })
}

And one of the arguments can be from another function (or say you want to build it with another function).

function getUrl() {
    return new Promise(function(resolve) {
        setTimeout(function() {
            resolve('http://www.google.com');
        }, 10);
    });
}

Now instead of calling them together all the time, you can wrap the original function and you get a new function.

var myRequest = injectify(requestSomething, 'url');

And inject the getter function.

myRequest.inject('url', getUrl);

Tip: you can do the above 2 lines in one step.

var myRequest = injectify(requestSomething, 'url').inject('url', getUrl);

Now you can run the wrapper, with only the other arguments (meaning that the new function has a different API with the original function).

myRequest('other', 'args').then(function(res) {
    // res.should.be.type('object');
    // res.should.have.property('statusCode', 200);
});

Why

This small tool solves a very specific problem, where:

  1. You are building a module (not an application where you could do whatever you want).
  2. And you are building a function that has some asynchronous dependencies.
  3. And you want to give it a default parameter while still give your user a chance to change it.

Without this tool it is usually hard to achieve the same thing but with this tool the code can be a bit more complex; your choice.

API

TODO

Variants

Return self

A variant that returns the context or this.

Can be used to build some callback based APIs.

However note that the dependencies are still promise based.

Return auto

A variant that returns either the promise or the context, depending on if a callback function is given.

Can be used to build something with both a promise API and a callback API.

However note that 1) the dependencies are still promise based and 2) the last argument is considered as a callback if it is a function and 3) we assume the callback will be properly handled.

Cached result (caching the promise)

TODO

Bonus

As a flow control tool

TODO