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

infect

v0.4.6

Published

Infect.js is a simple way to add the magic of dependency injection to any web project, regardless of the framework on which you choose to write your application. It's infectiously simple!

Downloads

116

Readme

Infect.js - simple dependency injection

Infect.js is a simple way to add the magic of dependency injection to any web project, regardless of the framework on which you choose to write your application. It's infectiously simple!

buy me a cup of coffee?

Features

  • Extremely lightweight (under 1kb min+gz)
  • Has no dependencies of its own
  • Supports module loading systems (RequireJS/AMD, Node.js) with browser global fallback.
  • 4 ways to inject your code (function, class, object, and assignment)
  • Works in every browser I've tested so far

Browser Support (verified)

  • Internet Explorer 6+
  • Chome 14+
  • Firefox 3.6+
  • Android
  • iOS

Installation

Download: Source | Minified

NPM: npm install infect

Bower: bower install infect

Getting Started

Registering a dependency

infect.set(String, Object|Function|Array|etc...)

A simple call to infect.set() with the name you want to use, and the mutable object you'd like to register will do the trick. In the example below we are using a function, but you can register any type of mutable value (Functions, Arrays, Objects, etc).

infect.set('Logger', function (str) {
	// prepend a time to every log line
	console.log((new Date()).toLocaleTimeString() + ' ==> ' + str);
});

Function Injection

infect.func(Function, Object)

When you're writing a function that needs one or more dependency, simply pass it to infect.func() and reference the dependencies as parameters that are prefixed with a dollar sign ($). All dependencies should be added to the end of the parameter list, and they are not expected when you call the function. Optionally you may pass in an object that you want to be used as this inside the function. If no object is provided, a blank object will be created for this purpose (the global scope will not be used).

var foo = infect.func(function (name, age, $Logger) {
	$Logger(name + ' is ' + age);
}, this);

foo('Joe', 27);

// CONSOLE
// 11:50:37 PM ==> Joe is 27

view on jsFiddle


Class Injection

infect.func(Function)

The infect.func() method can also support javascript constructors (classes). Just like above, all dependencies should be added to the end of the parameter list, and they are not expected when you new the constructor.

function Cat(name, $Logger) {
	$Logger(name + ' is a Cat');
	this.name = name;
}
Cat = infect.func(Cat);

var c = new Cat('Mr. Buttons');
infect.get('Logger')('is c a Cat? ' + (c instanceof Cat));

// CONSOLE
// 11:50:37 PM ==> Mr. Buttons is a Cat
// 11:50:37 PM ==> is c a Cat? true

view on jsFiddle


Object Injection

infect.obj(Object, Array)

Sometimes function injection may not work for you, but you'd still like an easy way to pull multiple dependencies into a single place for reference within your code. Object injection suits this nicely and can be done by simply calling infect.obj() with an object and an array of dependency names you'd like to have injected. Please node that any object can be injected except the global Window object (for obvious reasons). If you try to infect the global scope, infect will throw an error.

NOTE: You can reference the dependency with or without the dollar sign prefix ($). Regardless of how you reference it, the object will be injected with the prefixed version for consistency.

var INFECTED = infect.obj({}, ['$Logger']);
var ALSO_INFECTED = {};
infect.obj(ALSO_INFECTED, ['Logger']);

INFECTED.$Logger('foo!');
ALSO_INFECTED.$Logger('bar!');

// CONSOLE
// 11:50:37 PM ==> foo!
// 11:50:37 PM ==> bar!

view on jsFiddle


Assignment

infect.get(String)

As a simple way to reference your dependencies, you can pull them out and assign individual dependencies to variables. This is not recommended as it breaks the consistency of your code, but it is possible so I wanted to show an example of usage.

var log = infect.get('Logger');
log('foo bar!');

// CONSOLE
// 11:50:37 PM ==> foo bar!

view on jsFiddle


Minification and Obfuscation

Function.$infect = Array

When running your code through minification or obfuscation, the function parameters get renamed in order to save space. Obviously this wreaks havoc on Infect's ability to detect what values should be injected into the function at execution. To solve this you can simply add an array of dependency names to the $infect property of your function and infect will inject these values into the function.

var foo = infect.func(function (n, a, L) {
	L(n + ' is ' + a);
}, this);
foo.$infect = ['Logger'];

foo('Joe', 27);

// CONSOLE
// 11:50:37 PM ==> Joe is 27

view on jsFiddle


githalytics.com alpha