Comparing version 0.3.2 to 0.4.0
72
index.js
@@ -1,6 +0,6 @@ | ||
'use strict'; | ||
'use strict' | ||
// A dependency injection container, holding all modules, mocks and dependencies. | ||
function Flacon () { | ||
var modules = {}, notCached = {}; // `{} is …` is never true. | ||
var modules = {}, notCached = {} // `{} is …` is never true. | ||
@@ -10,59 +10,57 @@ | ||
var load = function (id, mocks) { // `mocks` is optional | ||
var module, deps, hasMocks; | ||
if ('string' !== typeof id) throw new Error('`id` must be a string.') | ||
if (Array.isArray(mocks) || 'object' !== typeof mocks) mocks = {} | ||
if ('string' !== typeof id) throw new Error('`id` must be a string.'); | ||
if (Array.isArray(mocks) || 'object' !== typeof mocks) mocks = {}; | ||
if (!modules[id]) throw new Error(id + ' has not been registered.') | ||
var module = modules[id] | ||
if (!modules[id]) throw new Error(id + ' has not been registered.'); | ||
else module = modules[id]; | ||
var deps = Array.isArray(module.factory.deps) ? module.factory.deps : [] | ||
var hasMocks = Object.keys(mocks).some(function (mock) { | ||
return deps.indexOf(mock) >= 0 | ||
}) | ||
hasMocks = Object.keys(mocks).length > 0; | ||
if (module.cache === notCached || mocks[id]) { | ||
// merge dependencies and mocks | ||
deps = module.deps.map(function (id) { | ||
var dep = load(id, mocks); | ||
if (hasMocks) { | ||
deps = deps.map(function (id) { // merge dependencies and mocks | ||
var dep = load(id, mocks) | ||
// For greater flexibility, the mocks are being called with the | ||
// dependency. They can then manipulate it or return something entirely new. | ||
if (mocks.hasOwnProperty(id)) return mocks[id](dep); | ||
else return dep; | ||
return mocks.hasOwnProperty(id) ? mocks[id](dep) : dep | ||
}); | ||
return module.factory.apply({}, deps) | ||
if (hasMocks) return module.factory.apply({}, deps); | ||
module.cache = module.factory.apply({}, deps); | ||
} else if (module.cache === notCached) { | ||
deps = deps.map(function (id) {return load(id, mocks)}) | ||
module.cache = module.factory.apply({}, deps) | ||
} | ||
return module.cache; | ||
}; | ||
return module.cache | ||
} | ||
load.flush = function (id) { | ||
if ('string' !== typeof id) throw new Error('`id` must be a string.'); | ||
if (!modules[id]) throw new Error(id + ' has not been registered.'); | ||
if ('string' !== typeof id) throw new Error('`id` must be a string.') | ||
if (!modules[id]) throw new Error(id + ' has not been registered.') | ||
modules[id].cache = notCached; | ||
return this; // for method chaining | ||
}; | ||
modules[id].cache = notCached | ||
return this // for method chaining | ||
} | ||
load.publish = function (id, deps, factory) { | ||
if ('string' !== typeof id) throw new Error('`id` must be a string.'); | ||
if (arguments.length === 2) { // `deps` is optional | ||
factory = deps; | ||
deps = []; | ||
} | ||
if ('function' !== typeof factory) throw new Error('`factory` must be a function.'); | ||
load.publish = function (id, factory) { | ||
if ('string' !== typeof id) throw new Error('`id` must be a string.') | ||
if ('function' !== typeof factory) throw new Error('`factory` must be a function.') | ||
if (modules[id]) throw new Error(id + ' has already been registered.'); | ||
else modules[id] = {deps: deps, factory: factory, cache: notCached}; | ||
if (modules[id]) throw new Error(id + ' has already been registered.') | ||
else modules[id] = {factory: factory, cache: notCached} | ||
return factory; // To make publishing *and* exporting a factory easier. | ||
}; | ||
return factory // To make publishing *and* exporting a factory easier. | ||
} | ||
return load; | ||
return load | ||
} | ||
if (module) module.exports = Flacon; | ||
if (module) module.exports = Flacon |
{ | ||
"name": "flacon", | ||
"description": "A hyperminimal dependency injection framework.", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "files": ["index.js", "readme.md"], |
# flacon | ||
*flacon* is a dependency injection container with a clean and tiny API. It helps you to test your components individually. | ||
***flacon* is a dependency injection container** with a clean and tiny API that helps you to test your components individually. | ||
- **Flexible** – It does *not* care about what a module factory returns. | ||
- **Unopinionated** – It does *not* interfere with `require` calls. | ||
- **Simple** – Have a look at [the source code](index.js). | ||
[![build status](https://img.shields.io/travis/derhuerst/flacon.svg)](https://travis-ci.org/derhuerst/flacon) | ||
@@ -69,7 +73,9 @@ [![dependency status](https://img.shields.io/david/derhuerst/flacon.svg)](https://david-dm.org/derhuerst/flacon#info=dependencies) | ||
container.publish('bar', ['foo'], function (foo) { | ||
var factory = function (foo) { | ||
return { | ||
value: function () { return foo.value() + 'bar' } | ||
}; | ||
}); | ||
}; | ||
factory.deps = ['foo']; | ||
container.publish('bar', factory); | ||
``` | ||
@@ -106,3 +112,5 @@ | ||
*Note: In a mock function, make sure to never manipulate given module, always return a new one!* | ||
### `flush` | ||
@@ -131,8 +139,7 @@ | ||
### `flacon.publish(id, [deps], factory)` | ||
### `flacon.publish(id, factory)` | ||
Registers a module by `id`. Returns the module's `factory`. | ||
Registers a module by `id`. Reads the module's dependencies from `factory.deps`. Returns the module's `factory`. | ||
- `id`: The identifier, unique to the container. | ||
- `deps`: An optional array of dependency `id`s. Their corresponding modules will be passed into `factory`. | ||
- `factory`: A function, taking the dependencies, that returns the module. | ||
@@ -139,0 +146,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7656
155
43