injecty
injecty is a micro library for dependency injection and inversion of control container in JavaScript. It's dependency-free, light and small (~200 SLOC). It was designed to be embedded in frameworks or libraries
It's intimately inspired in AngularJS DI and provides useful features such as autodiscover injections from arguments using pattern matching, creating multiple containers with inheritance support between them, AngularJS-style injections based on array notation and more
injecty is written in Wisp, a Clojure-like language which transpiles into plain JavaScript. It exploits functional programming common patterns such as lambda lifting, pure functions, higher-order functions, function composition and more
Installation
Node.js
npm install injecty
Browser
Via Bower
bower install injecty
Via Component
component install h2non/injecty
Or loading the script remotely
Environments
It works in any ES5 compliant engine
- Node.js
- Chrome >= 5
- Firefox >= 3
- Safari >= 5
- Opera >= 12
- IE >= 9
Basic usage
var injecty =
injectyinjecty { return { var xhr = xhr xhr { if xhrreadyState === 4 } xhr return xhr }} var get = injecty
API
injecty.container([parent])
Creates a new container. Optionally it can inherit from another container
// register a dependency in the global built-in containerinjecty// creates new container which inherits from globalvar container = injecty// check it was registeredcontainer // -> true
injecty.get(name)
Alias: require
Retrieve a registered dependency by its name
injecty // -> {MathConstructor...}
injecty.register(name, value)
Alias: set
Register a new dependency in the container
injectyinjecty // -> true
You can also register functions that require injections
injectyinjectyvar time = injectyconsole // -> 1405170246959
Different ways to declare injections for consistency in browsers
Using the injection array notation
injecty
Setting the $inject
property in the function object
{ return m}random$inject = 'Math'injecty
injecty.invoke(fn/array)
Invoke a function injecting requested dependencies. Optinally you can supply the arguments to inject as array notation
var time = injectyconsole // -> 1405170246959
Using the array injection notation, useful for minification
var time = injectyconsole // -> 1405170246959
injecty.inject(fn/array)
Inject dependencies and return the partial function
var time = injecty
injecty.annotate(fn/array)
Returns an array of names which the given function is requesting for injection
var injectables = injectyconsole // -> ['Math', 'Date']
{ ...}fn$inject = 'Math' 'Date'var injectables = injectyconsole // -> ['Math', 'Date']
injecty.injectable(name)
Checks if a dependency was already registered and it's available to be injected
injecty.satisfies(fn)
Checks if can safisty all the requested dependecies to inject
injectinjectinject // -> true
injecty.remove(name)
Remove a registered dependency from the container
injecty // -> false
injecty.flush()
Flush all registered dependencies in the container
injecty // -> false
injecty.dependencies()
Get an array
of values with the registered dependency names in the container
Contributing
Wanna help? Cool! It will be appreciated :)
You must add new test cases for any new feature or refactor you do, always following the same design/code patterns that already exist
Tests specs are completely written in Wisp language. Take a look to the language documentation if you are new with it. You should follow the Wisp language coding conventions
Development
Only node.js is required for development
Clone this repository
$ git clone https://github.com/h2non/injecty.git && cd injecty
Install dependencies
$ npm install
Compile code
$ make compile
Run tests
$ make test
Browser sources bundle generation
$ make browser
License
MIT - Tomas Aparicio