+15
-12
@@ -1,16 +0,15 @@ | ||
| var is = require('is'); | ||
| // A dependency injection container, holding all modules, mocks and dependencies. | ||
| var Flacon = function () { | ||
| var modules = {}, | ||
| notCached = {}; // `{} is …` is never true. | ||
| var Container = function () { | ||
| var modules = {}, notCached = {}; | ||
| var load = function (id, mocks) { | ||
| var module, deps; | ||
| if (!is.string(id)) throw new Error('`id` must be a string.'); | ||
| if (!is.object(mocks)) mocks = {}; | ||
| if ('string' !== typeof id) throw new Error('`id` must be a string.'); | ||
| // `mocks` is optional | ||
| if (Array.isArray(mocks) || 'object' !== typeof mocks) mocks = {}; | ||
@@ -21,4 +20,7 @@ if (!modules[id]) throw new Error(id + ' has not been registered.'); | ||
| if (module.cache === notCached) { | ||
| // merge dependencies and mocks | ||
| deps = module.deps.map(function (id) { | ||
| var dep = load(id); | ||
| // 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); | ||
@@ -36,8 +38,8 @@ else return dep; | ||
| var publish = function (id, deps, factory) { | ||
| if (!is.string(id)) throw new Error('`id` must be a string.'); | ||
| if (arguments.length === 2) { | ||
| if ('string' !== typeof id) throw new Error('`id` must be a string.'); | ||
| if (arguments.length === 2) { // `deps` is optional | ||
| factory = deps; | ||
| deps = []; | ||
| } | ||
| if (!is.fn(factory)) throw new Error('`factory` must be a function.'); | ||
| if ('function' !== typeof factory) throw new Error('`factory` must be a function.'); | ||
@@ -51,2 +53,3 @@ if (modules[id]) throw new Error(id + ' has already been registered.'); | ||
| // To make publishing *and* exporting a factory easier, we return it here. | ||
| return factory; | ||
@@ -63,2 +66,2 @@ }; | ||
| module.exports = Container; | ||
| if (module) module.exports = Flacon; |
+10
-11
| { | ||
| "name": "flacon", | ||
| "description": "A hyperminimal dependency injection framework.", | ||
| "version": "0.1.0", | ||
| "version": "0.2.0", | ||
| "main": "index.js", | ||
@@ -12,16 +12,15 @@ "files": ["index.js", "readme.md"], | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "is": "^3.1" | ||
| }, | ||
| "devDependencies": { | ||
| "coffee-script": "^1.10", | ||
| "mocha": "^2.3", | ||
| "is": "^3.1", | ||
| "sinon": "^1.17" | ||
| "coffee-script": "^1.10", | ||
| "mocha": "^2.3", | ||
| "is": "^3.1", | ||
| "sinon": "^1.17", | ||
| "uglify-js": "^2.6" | ||
| }, | ||
| "scripts": { | ||
| "test": "mocha -R min --compilers coffee:coffee-script/register test/index.coffee", | ||
| "test:watch": "mocha -R min -w --compilers coffee:coffee-script/register test/index.coffee", | ||
| "prepublish": "npm test" | ||
| "test": "mocha -R min --compilers coffee:coffee-script/register test/index.coffee", | ||
| "test:watch": "mocha -R min -w --compilers coffee:coffee-script/register test/index.coffee", | ||
| "prepublish": "npm test", | ||
| "dist": "uglifyjs index.js -cm --preamble '// github.com/derhuerst/flacon' -o index.min.js" | ||
| } | ||
| } |
+2
-2
@@ -45,5 +45,5 @@ # flacon | ||
| ```js | ||
| var Container = require('flacon'); | ||
| var Flacon = require('flacon'); | ||
| module.exports = new Container(); | ||
| module.exports = new Flacon(); | ||
| ``` | ||
@@ -50,0 +50,0 @@ |
5881
9.56%0
-100%44
15.79%5
25%