New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

infect

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

infect

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!

  • 0.3.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21
decreased by-41.67%
Maintainers
1
Weekly downloads
 
Created
Source

Infect.js

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!

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)

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 prepended 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.

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 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 prepended dollar sign ($) but, in either case, the object will be infected with the prepended version.

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


githalytics.com alpha

Keywords

FAQs

Package last updated on 03 Dec 2013

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc