boxed-injector
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -0,91 +1,50 @@ | ||
declare namespace Injector { | ||
export interface BaseOptions { | ||
depends: Array<string> | string | ||
} | ||
export class Injector { | ||
constructor(); | ||
export interface FactoryOptions extends BaseOptions {} | ||
export interface RegisterOptions extends BaseOptions {} | ||
create(name: any, otherArgs: any, ...args: any[]): any; | ||
factory(name: string, _factory: any): any; | ||
get(name: string): any; | ||
inject(Factory: any): any; | ||
middleware(name: string, method: any): any; | ||
register(name: string, instance: any): any; | ||
export interface Entity { | ||
factory: any, | ||
depends: Array<string>, | ||
options?: { | ||
function: boolean | ||
} | ||
} | ||
} | ||
export namespace Injector { | ||
namespace prototype { | ||
function create(name: string, otherArgs: any, ...args: any[]): any; | ||
declare class Injector { | ||
constructor(); | ||
function factory(name: string, _factory: any): any; | ||
set(key: any, value: any): this; | ||
function get(name: string): any; | ||
has(key: any): boolean; | ||
function inject(Factory: any): any; | ||
factory(name: string, factory: any, options?: Injector.FactoryOptions): this; | ||
function middleware(name: string, method: any): any; | ||
register(name: string, factory: any, options?: Injector.RegisterOptions): this; | ||
function register(name: string, instance: any): any; | ||
inject(entity: Injector.Entity): any; | ||
namespace create { | ||
const prototype: { | ||
}; | ||
create(name: string, otherArgs: any): any; | ||
} | ||
graph(name: string, nested: boolean): Array<string>; | ||
namespace factory { | ||
const prototype: { | ||
}; | ||
get(name: string): any; | ||
} | ||
namespace get { | ||
const prototype: { | ||
}; | ||
} | ||
namespace inject { | ||
const prototype: { | ||
}; | ||
} | ||
namespace middleware { | ||
const prototype: { | ||
}; | ||
} | ||
namespace register { | ||
const prototype: { | ||
}; | ||
} | ||
} | ||
middleware(name: string, method: Function): this; | ||
middleware(method: Function): this; | ||
} | ||
export namespace Locator { | ||
function get(): Injector; | ||
declare namespace Locator { | ||
function set(injector: Injector): any; | ||
function set(injector: Injector): void; | ||
namespace get { | ||
const prototype: { | ||
}; | ||
} | ||
namespace set { | ||
const prototype: { | ||
}; | ||
} | ||
function get(): Injector; | ||
} | ||
export { | ||
Injector, | ||
Locator | ||
}; |
'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -14,2 +12,3 @@ | ||
var assert = require('assert'); | ||
var autoBind = require('auto-bind'); | ||
@@ -25,12 +24,3 @@ var Injector = function () { | ||
this.register = this.register.bind(this); | ||
this.middleware = this.middleware.bind(this); | ||
this.factory = this.factory.bind(this); | ||
this.inject = this.inject.bind(this); | ||
this.get = this.get.bind(this); | ||
this._ensureDistinct = this._ensureDistinct.bind(this); | ||
this._applyMiddleware = this._applyMiddleware.bind(this); | ||
this._initMiddleware = this._initMiddleware.bind(this); | ||
this.create = this.create.bind(this); | ||
this.graph = this.graph.bind(this); | ||
autoBind(this); | ||
} | ||
@@ -86,2 +76,18 @@ | ||
}, { | ||
key: 'set', | ||
value: function set(key, value) { | ||
if (!this.has(key)) { | ||
return this.register(key, value); | ||
} | ||
Object.assign(this.instances[key], { | ||
instance: value | ||
}); | ||
return this; | ||
} | ||
}, { | ||
key: 'has', | ||
value: function has(key) { | ||
return Boolean(this.factories[key] || this.instances[key]); | ||
} | ||
}, { | ||
key: 'factory', | ||
@@ -182,3 +188,7 @@ value: function factory(name, _factory, options) { | ||
if (!Array.isArray(name) && !this.factories[name] && !this.instances[name]) { | ||
var defaultObj = { all: [], hash: {} }; | ||
if (!Array.isArray(name) && !this.has(name)) { | ||
if (nested) { | ||
return defaultObj; | ||
} | ||
return []; | ||
@@ -203,2 +213,3 @@ } | ||
var child = _this4.graph(elem, true); | ||
console.log('this bout to fail', child); | ||
child.all.forEach(function (childDep) { | ||
@@ -209,6 +220,3 @@ add(childDep); | ||
return obj; | ||
}, { | ||
all: [], | ||
hash: {} | ||
}); | ||
}, defaultObj); | ||
@@ -235,3 +243,3 @@ if (!nested) { | ||
self._applyMiddleware(entity, 'before'); // run before middleware on factory - only runs once | ||
self.instances[name] = _extends({}, self.factories[name], { | ||
self.instances[name] = Object.assign({}, self.factories[name], { | ||
instance: self.inject(entity) | ||
@@ -238,0 +246,0 @@ }); |
@@ -11,3 +11,3 @@ 'use strict'; | ||
* to run standalone: | ||
* mocha --require babel-register lib/Injector/Injector.test.js --watch | ||
* mocha --require babel-register src/Injector/Injector.test.js --watch | ||
*/ | ||
@@ -32,5 +32,11 @@ | ||
it('should be able to register instances', function () { | ||
injector.register('foo', 'bar', { depends: 'bar' }); | ||
expect(injector.graph('foo')).toEqual(['bar']); | ||
}); | ||
it('should be able to register instances', function () { | ||
injector.register('foo', 'bar'); | ||
expect(injector.instances.foo.instance).toEqual('bar', 'was expecting to be able to register instances.'); | ||
}); | ||
it('should be able to register booleans', function () { | ||
@@ -228,2 +234,6 @@ injector.register('foo', true); | ||
describe('graph', function () { | ||
it('should return empty [] for undef.', function () { | ||
var result = injector.graph('asdf'); | ||
expect(result).toEqual([]); | ||
}); | ||
it('should get a graph', function () { | ||
@@ -356,2 +366,22 @@ injector.register('asdf', 'asdf'); | ||
}); | ||
describe('set', function () { | ||
var name = 'foo'; | ||
var thing = 'abcd'; | ||
var otherThing = 'abcd'; | ||
it('doesn\'t exist', function () { | ||
injector.set(name, thing); | ||
expect(injector.get(name)).toEqual(thing); | ||
injector.set(name, otherThing); | ||
expect(injector.get(name)).toEqual(otherThing); | ||
}); | ||
}); | ||
describe('has', function () { | ||
var name = 'foo'; | ||
var thing = 'abcd'; | ||
it('doesn\'t exist', function () { | ||
injector.factory(name, thing); | ||
expect(injector.has(name)).toEqual(true); | ||
expect(injector.has('bar')).toEqual(false); | ||
}); | ||
}); | ||
}); |
@@ -8,3 +8,3 @@ 'use strict'; | ||
/** | ||
* mocha lib/Injector/integration.test.js --watch | ||
* mocha src/Injector/integration.test.js --watch | ||
*/ | ||
@@ -11,0 +11,0 @@ |
@@ -10,3 +10,3 @@ 'use strict'; | ||
* to run standalone: | ||
* mocha --require babel-register lib/Locator/Locator.test.js --watch | ||
* mocha --require babel-register src/Locator/Locator.test.js --watch | ||
*/ | ||
@@ -13,0 +13,0 @@ |
{ | ||
"name": "boxed-injector", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Dependency Injection Tools", | ||
@@ -24,34 +24,21 @@ "homepage": "", | ||
"devDependencies": { | ||
"@istanbuljs/nyc-config-babel": "^1.2.2", | ||
"babel-cli": "^6.24.1", | ||
"babel-core": "^6.11.4", | ||
"babel-eslint": "^7.0.0", | ||
"babel-loader": "^6.2.5", | ||
"babel-eslint": "^6.1.2", | ||
"babel-plugin-array-includes": "^2.0.3", | ||
"babel-plugin-istanbul": "^4.1.4", | ||
"babel-plugin-transform-class-properties": "^6.11.5", | ||
"babel-plugin-transform-decorators-legacy": "^1.3.4", | ||
"babel-plugin-transform-object-assign": "^6.8.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.8.0", | ||
"babel-polyfill": "^6.13.0", | ||
"babel-preset-es2015": "^6.14.0", | ||
"babel-preset-es2017": "^6.14.0", | ||
"babel-preset-react": "^6.11.1", | ||
"babel-preset-react-hmre": "^1.1.1", | ||
"babel-preset-stage-0": "^6.5.0", | ||
"babel-register": "^6.9.0", | ||
"del": "^2.0.2", | ||
"coveralls": "^2.13.1", | ||
"eslint": "^3.1.1", | ||
"eslint-config-xo-space": "^0.15.0", | ||
"eslint-plugin-babel": "^3.3.0", | ||
"eslint-plugin-react": "^6.6.0", | ||
"expect": "^1.20.2", | ||
"gulp": "^3.9.0", | ||
"gulp-babel": "^6.1.2", | ||
"gulp-coveralls": "^0.1.0", | ||
"gulp-eslint": "^3.0.1", | ||
"gulp-exclude-gitignore": "^1.0.0", | ||
"gulp-istanbul": "^1.0.0", | ||
"gulp-line-ending-corrector": "^1.0.1", | ||
"gulp-mocha": "^3.0.1", | ||
"gulp-nsp": "^2.1.0", | ||
"gulp-plumber": "^1.0.0", | ||
"isparta": "^4.0.0", | ||
"mocha": "^3.5.0", | ||
"nyc": "^11.1.0", | ||
"sinon": "^1.17.6" | ||
@@ -61,8 +48,10 @@ }, | ||
"scripts": { | ||
"prepublish": "gulp prepublish", | ||
"test": "gulp", | ||
"test:watch": "gulp watch", | ||
"test:injector": "mocha --require babel-register ./lib/injector/Injector.test.js" | ||
"pretest": "NODE_ENV=test ./node_modules/.bin/babel src --out-dir dist", | ||
"prepublish": "make prepublish", | ||
"test": "make test", | ||
"test:watch": "make watch" | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"auto-bind": "^1.1.0" | ||
} | ||
} |
@@ -1,2 +0,5 @@ | ||
# boxed-injector [![NPM version][npm-image]][npm-url] [![Build Status](https://travis-ci.org/giddyinc/boxed-injector.svg?branch=master)](https://travis-ci.org/giddyinc/boxed-injector) [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage Status](https://coveralls.io/repos/github/giddyinc/boxed-injector/badge.svg?branch=master)](https://coveralls.io/github/giddyinc/boxed-injector?branch=master) | ||
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] | ||
# boxed-injector | ||
> Dependency Injection Tools | ||
@@ -168,2 +171,8 @@ | ||
// ['d', 'c', 'b', 'a'] | ||
// true or false if the injector has a service/factory registered. | ||
injector.has('foo'); | ||
// like injector.register, but actually replaces the instance (no guard) | ||
injector.set('foo', 'bar'); | ||
``` | ||
@@ -170,0 +179,0 @@ |
Sorry, the diff of this file is not supported yet
20
756
195
36395
1
+ Addedauto-bind@^1.1.0
+ Addedauto-bind@1.2.1(transitive)