From 5c18b67b9e9d7099b39a9d0e38c92293baea0d84 Mon Sep 17 00:00:00 2001 From: Nicolas Straub Date: Sat, 12 Mar 2016 17:23:58 -0300 Subject: [PATCH] restructuring and first functional Grunt config --- Gruntfile.js | 5 +- README.md | 13 +++-- Tests/karma.conf.js | 2 +- Tests/karma.dist.min.conf.js | 2 +- bower.json | 2 +- dist/inject.js | 89 ++++++++++++++++------------- dist/inject.min.js | 4 +- package.json | 106 +++++++++++++++++------------------ src/inject.constructor.js | 2 + src/inject.js | 16 ++++++ src/inject.registration.js | 52 +++++------------ src/inject.util.js | 17 ++++++ src/require.shim.js | 6 +- 13 files changed, 166 insertions(+), 150 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 4db4193..0a4f48a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -58,16 +58,13 @@ module.exports = function(grunt) { watch: { scripts: { files: ['Gruntfile.js', 'src/*.js', 'Tests/*.js', 'Tests/**/*.coffee'], - tasks: ['jshint', 'karma:unit', 'concat', 'uglify'] + tasks: ['jshint', 'karma:unit', 'concat', 'uglify', 'karma:dist', 'karma:min'], } }, karma: { unit: { configFile: 'Tests/karma.conf.js' }, - full: { - configFile: 'Tests/karma.full.conf.js' - }, dist: { configFile: 'Tests/karma.dist.conf.js' }, diff --git a/README.md b/README.md index 9c56e43..06600b9 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ simply download inject.js or inject.min.js from the dist folder and include it i > **Note:** As of version 0.3 InjectJS includes a grunt build process to lint, test, merge and minify all the code into a single inject.js file (and an inject.min.js file, of course). If you want to tinker about with the different aspects of the library simply clone it and look into the src dir. -> **Note:** InjectJS depends on Lodash ([https://lodash.com/](https://lodash.com/)). You can probably use it with underscore as well, but the framework is developed and test using Lodash, so there are no assurances. +> **Note:** InjectJS depends on Lodash ([https://lodash.com/](https://lodash.com/)). You can probably use it with underscore as well, but the library is developed and tested using Lodash, so there are no assurances. ## Tests @@ -130,7 +130,7 @@ So let's complicate things a bit... # Roadmap -The current version, 0.2, has the following features: +The current version, 0.3, has the following features: - [registering types](#registration-type) - [registering providers](#registration-provider) @@ -141,12 +141,13 @@ The current version, 0.2, has the following features: - [injecting types into methods](#injection-inject) - Singleton, Transient and State lifetimes. - test facilities: [fakes](#testing-fakes) and [harnesses](#testing-harness) +- Code modularization and Grunt build process. To sum it up, it provides basic dependency injection capabilities and the ability to use these dependencies in a test environment. So here are the next planned releases: -- 0.3 Code modularization and Grunt build process. + - 0.4 Root and parent lifetimes. - 0.5 Preprocessor for dealing with minifiers. - 0.6 abstract types @@ -166,7 +167,7 @@ The syntax for this framework takes from and expands the syntax used by AngularJ **signatures** -*registerType(name, type, [lifetime = 'transient'], [provider]) : void* +*registerType(name, type, [lifetime = 'transient'], [provider]) : void* *registerType(name, type: [2-\*], [lifetime = 'transient'], [provider]) : void* **Parameters** @@ -201,7 +202,7 @@ Allows you to register a provider which can be instantiated by the injector and **signatures** -*registerProvider(name, provider) : void* +*registerProvider(name, provider) : void* *registerProvider(name, provider: [2-\*]) : void* **Parameters** @@ -221,7 +222,7 @@ Allows you to register the provider that gets invoked when `injector.run()` is c **signatures** -*registerMain(provider) : void* +*registerMain(provider) : void* *registerMain(provider: [2-\*]) : void* **Parameters** diff --git a/Tests/karma.conf.js b/Tests/karma.conf.js index 6ea70a2..53888c0 100644 --- a/Tests/karma.conf.js +++ b/Tests/karma.conf.js @@ -42,7 +42,7 @@ module.exports = function (config) { // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { - '*.js': ['coverage'], + 'src/*.js': ['coverage'], '**/*.coffee': ['coffee'] }, diff --git a/Tests/karma.dist.min.conf.js b/Tests/karma.dist.min.conf.js index 3e072e0..8f4fafc 100644 --- a/Tests/karma.dist.min.conf.js +++ b/Tests/karma.dist.min.conf.js @@ -25,7 +25,7 @@ module.exports = function (config) { files: [ 'node_modules/lodash/index.js', 'Tests/register.method.js', - 'dist/injectjs.min.js', + 'dist/inject.min.js', 'Tests/**/*.spec.coffee', 'bower_components/sinonjs/sinon.js', 'bower_components/jasmine-sinon/lib/jasmine-sinon.js' diff --git a/bower.json b/bower.json index 2a28e80..1670469 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "injectjs", - "version": "0.0.0", + "version": "0.3.0", "authors": [ "Nicolas Straub" ], diff --git a/dist/inject.js b/dist/inject.js index 34d8f6b..cab57be 100644 --- a/dist/inject.js +++ b/dist/inject.js @@ -1,4 +1,4 @@ -/*! injectjs - v1.0.0 - 2016-03-11 +/*! injectjs - vv0.3.0 - 2016-03-12 * https://github.com/nstraub/injectjs * Copyright (c) 2016 ; Licensed */ 'use strict'; @@ -10,13 +10,31 @@ function Injector() { this.state = {}; } +Injector.prototype.DEFAULT_LIFETIME = 'transient'; + /* globals window: false */ /* exported lifetimes */ /* exported old_injector */ +/* globals get_dependency_names*/ + var lifetimes = ['singleton', 'transient', 'root', 'parent', 'state']; var old_injector = window.injector; +Injector.prototype.build_anonymous_descriptor = function (name) { // for when inject is called with an anonymous function + if (typeof name === 'function') { + return { + type: name, + dependencies: get_dependency_names(name) + }; + } else { + return { + type: name.pop(), + dependencies: name + }; + } +}; + /*---------------------- -- Injection Methods -- ----------------------*/ @@ -141,29 +159,20 @@ Injector.prototype.provide_provider = function(dependency_providers, type) { /* globals Injector: false */ /* globals lifetimes: false */ +/* globals get_dependency_names*/ -var get_dependency_names = (function () { - var dependency_pattern = /^function ?\w* ?\(((?:\w+|(?:, ?))+)\)/; - var separatorPattern = /, ?/; - return function get_dependency_names(type) { - var serialized_type = type.toString(); - var serialized_dependencies; - - if (serialized_dependencies = dependency_pattern.exec(serialized_type)) { - return serialized_dependencies[1].split(separatorPattern); - } else { - return null; - } - }; -}()); +function assertLifetime (lifetime) { + if (!~lifetimes.indexOf(lifetime)) { + throw 'invalid lifetime "' + lifetime + '" provided. Valid lifetimes are singleton, transient, instance and parent'; + } +} +Injector.prototype.currentHashCode = 1; Injector.prototype.registerType = function (name, type, lifetime, provider) { - lifetime = lifetime || 'transient'; + lifetime = lifetime || this.DEFAULT_LIFETIME; - if (!~lifetimes.indexOf(lifetime)) { - throw 'invalid lifetime "' + lifetime + '" provided. Valid lifetimes are singleton, transient, instance and parent'; - } + assertLifetime(lifetime); this._register('types', name, type, lifetime); @@ -181,20 +190,18 @@ Injector.prototype.registerMain = function (main) { }; Injector.prototype.registerFake = function (name, type, lifetime) { - lifetime = lifetime || 'transient'; + lifetime = lifetime || this.DEFAULT_LIFETIME; - if (!~lifetimes.indexOf(lifetime)) { - throw 'invalid lifetime "' + lifetime + '" provided. Valid lifetimes are singleton, transient, instance and parent'; - } + assertLifetime(lifetime); this._register('fakes', name, type, lifetime); }; Injector.prototype._register = function (where, name, type, lifetime) { - var realType, dependencies; - var destination = this[where]; - if (typeof destination === 'undefined') { + var realType, dependencies, destination; + + if (typeof (destination = this[where]) === 'undefined') { throw 'invalid destination "' + where + '" provided. Valid destinations are types, providers, fakes and main'; } @@ -227,19 +234,6 @@ Injector.prototype._register = function (where, name, type, lifetime) { } destination[name] = result; }; -Injector.prototype.build_anonymous_descriptor = function (name) { // for when inject is called with an anonymous function - if (typeof name === 'function') { - return { - type: name, - dependencies: get_dependency_names(name) - }; - } else { - return { - type: name.pop(), - dependencies: name - }; - } -}; /* global Injector */ Injector.prototype.harness = function (func) { @@ -260,6 +254,23 @@ Injector.prototype.flushFakes = function () { /* globals injector: false */ /* globals old_injector: false */ /* globals window: false */ +/* exported get_dependency_names*/ + +var get_dependency_names = (function () { + var dependency_pattern = /^function ?\w* ?\(((?:\w+|(?:, ?))+)\)/; + var separatorPattern = /, ?/; + return function get_dependency_names(type) { + var serialized_type = type.toString(); + var serialized_dependencies; + + if (serialized_dependencies = dependency_pattern.exec(serialized_type)) { + return serialized_dependencies[1].split(separatorPattern); + } else { + return null; + } + }; +}()); + Injector.prototype.getType = function (name) { var type = this.fakes[name] || this.types[name]; diff --git a/dist/inject.min.js b/dist/inject.min.js index 8470d00..bfdbdb7 100644 --- a/dist/inject.min.js +++ b/dist/inject.min.js @@ -1,4 +1,4 @@ -/*! injectjs - v1.0.0 - 2016-03-11 +/*! injectjs - vv0.3.0 - 2016-03-12 * https://github.com/nstraub/injectjs * Copyright (c) 2016 ; Licensed */ -"use strict";var injector=function(){function a(){this.types={},this.providers={},this.fakes={},this.state={}}function b(a,c){var d,e;if("string"==typeof a){if(d=this.fakes[a]||this.types[a]||this.providers[a],this.cache[a]&&this.cache[a].hashCode===d.hashCode)return this.cache[a]}else d=this.build_anonymous_descriptor(a);if(!d){if(c)return null;throw'There is no dependency named "'+a+'" registered.'}return d.provider&&d.provider!==c?b.call(this,d.provider,a):(e={},_.each(d.dependencies,function(c){e[c]=b.call(this,c,a)},this),this.build_provider(a,d,e))}function c(a,b){var c=this,d={};return _.each(b,function(a,b){d[b]=function(){return a}}),_.assign(a,d),_.map(a,function(a,b){if(!a)throw'There is no dependency named "'+b+'" registered.';return a.call(c)})}function d(){injector.clearState()}var e=["singleton","transient","root","parent","state"],f=window.injector;a.prototype.inject=function(a){return b.call(this,a)},a.prototype.get=function(a,b,c){var d=this.inject(a);return b?d.call(b,c):d(c)},a.prototype.run=function(a,b){if(!this.providers.main)throw"No main method registered. Please register one by running injector.registerMain() before running the app";return this.get("main",a,b)},a.prototype.cache={};var g=Object.create;a.prototype.provide_transient=function(a,b){return function(d){var e=g(a.prototype);return a.apply(e,c(b,d)),e}},a.prototype.provide_singleton=function(a,b,c,d){var e=this;if(!d[a]){var f=this.provide_transient(b,c);d[a]=function(b){return"function"==typeof f&&(f=f(b),e.cache[a]=d[a]=function(){return f}),f}}return d[a]},a.prototype.provide_provider=function(a,b){return function(d){var e=c.call(this,a,d);return b.apply(this,e)}},function(){var b={};a.prototype.build_provider=function(a,c,d){if(!c.lifetime)return this.cache[a]=this.provide_provider(d,c.type);switch(c.lifetime){case"singleton":return this.cache[a]=this.provide_singleton(a,c.type,d,b);case"state":return this.cache[a]=this.provide_singleton(a,c.type,d,this.state);default:return this.cache[a]=this.provide_transient(c.type,d)}}}();var h=function(){var a=/^function ?\w* ?\(((?:\w+|(?:, ?))+)\)/,b=/, ?/;return function(c){var d,e=c.toString();return(d=a.exec(e))?d[1].split(b):null}}();return a.prototype.registerType=function(a,b,c,d){if(c=c||"transient",!~e.indexOf(c))throw'invalid lifetime "'+c+'" provided. Valid lifetimes are singleton, transient, instance and parent';this._register("types",a,b,c),d&&(this.types[a].provider=d)},a.prototype.registerProvider=function(a,b){this._register("providers",a,b)},a.prototype.registerMain=function(a){this._register("providers","main",a)},a.prototype.registerFake=function(a,b,c){if(c=c||"transient",!~e.indexOf(c))throw'invalid lifetime "'+c+'" provided. Valid lifetimes are singleton, transient, instance and parent';this._register("fakes",a,b,c)},a.prototype._register=function(a,b,c,d){var e,f,g=this[a];if("undefined"==typeof g)throw'invalid destination "'+a+'" provided. Valid destinations are types, providers, fakes and main';if("string"!=typeof b||""===b)throw"Type must have a name";if(!c)throw"no type was passed";if("function"==typeof c?(f=h(c),e=c):(e=c.pop(),f=c),"function"!=typeof e)throw"no type was passed";var i={name:b,type:e,dependencies:f,hashCode:this.currentHashCode++};d&&(i.lifetime=d),g[b]=i},a.prototype.build_anonymous_descriptor=function(a){return"function"==typeof a?{type:a,dependencies:h(a)}:{type:a.pop(),dependencies:a}},a.prototype.harness=function(a){var b=this;return function(c){return b.inject(a)(c)}},a.prototype.removeFake=function(a){delete this.fakes[a]},a.prototype.flushFakes=function(){this.fakes={}},a.prototype.getType=function(a){var b=this.fakes[a]||this.types[a];return b?b.type:null},a.prototype.extend=function(a,b){var c=this.types[a];if(!c)throw'No type "'+a+'" found.';b.prototype=this.get(a)},a.prototype.noConflict=function(){window.injector=f},a.prototype.removeDefaultListener=function(){window.removeEventListener("hashchange",d)},a.prototype.clearState=function(){this.state={},_.each(this.types,function(a,b){"state"===a.lifetime&&delete this.cache[b]},this)},window.addEventListener("hashchange",d),new a}(); \ No newline at end of file +"use strict";var injector=function(){function a(){this.types={},this.providers={},this.fakes={},this.state={}}function b(a,c){var d,e;if("string"==typeof a){if(d=this.fakes[a]||this.types[a]||this.providers[a],this.cache[a]&&this.cache[a].hashCode===d.hashCode)return this.cache[a]}else d=this.build_anonymous_descriptor(a);if(!d){if(c)return null;throw'There is no dependency named "'+a+'" registered.'}return d.provider&&d.provider!==c?b.call(this,d.provider,a):(e={},_.each(d.dependencies,function(c){e[c]=b.call(this,c,a)},this),this.build_provider(a,d,e))}function c(a,b){var c=this,d={};return _.each(b,function(a,b){d[b]=function(){return a}}),_.assign(a,d),_.map(a,function(a,b){if(!a)throw'There is no dependency named "'+b+'" registered.';return a.call(c)})}function d(a){if(!~f.indexOf(a))throw'invalid lifetime "'+a+'" provided. Valid lifetimes are singleton, transient, instance and parent'}function e(){injector.clearState()}a.prototype.DEFAULT_LIFETIME="transient";var f=["singleton","transient","root","parent","state"],g=window.injector;a.prototype.build_anonymous_descriptor=function(a){return"function"==typeof a?{type:a,dependencies:i(a)}:{type:a.pop(),dependencies:a}},a.prototype.inject=function(a){return b.call(this,a)},a.prototype.get=function(a,b,c){var d=this.inject(a);return b?d.call(b,c):d(c)},a.prototype.run=function(a,b){if(!this.providers.main)throw"No main method registered. Please register one by running injector.registerMain() before running the app";return this.get("main",a,b)},a.prototype.cache={};var h=Object.create;a.prototype.provide_transient=function(a,b){return function(d){var e=h(a.prototype);return a.apply(e,c(b,d)),e}},a.prototype.provide_singleton=function(a,b,c,d){var e=this;if(!d[a]){var f=this.provide_transient(b,c);d[a]=function(b){return"function"==typeof f&&(f=f(b),e.cache[a]=d[a]=function(){return f}),f}}return d[a]},a.prototype.provide_provider=function(a,b){return function(d){var e=c.call(this,a,d);return b.apply(this,e)}},function(){var b={};a.prototype.build_provider=function(a,c,d){if(!c.lifetime)return this.cache[a]=this.provide_provider(d,c.type);switch(c.lifetime){case"singleton":return this.cache[a]=this.provide_singleton(a,c.type,d,b);case"state":return this.cache[a]=this.provide_singleton(a,c.type,d,this.state);default:return this.cache[a]=this.provide_transient(c.type,d)}}}(),a.prototype.currentHashCode=1,a.prototype.registerType=function(a,b,c,e){c=c||this.DEFAULT_LIFETIME,d(c),this._register("types",a,b,c),e&&(this.types[a].provider=e)},a.prototype.registerProvider=function(a,b){this._register("providers",a,b)},a.prototype.registerMain=function(a){this._register("providers","main",a)},a.prototype.registerFake=function(a,b,c){c=c||this.DEFAULT_LIFETIME,d(c),this._register("fakes",a,b,c)},a.prototype._register=function(a,b,c,d){var e,f,g;if("undefined"==typeof(g=this[a]))throw'invalid destination "'+a+'" provided. Valid destinations are types, providers, fakes and main';if("string"!=typeof b||""===b)throw"Type must have a name";if(!c)throw"no type was passed";if("function"==typeof c?(f=i(c),e=c):(e=c.pop(),f=c),"function"!=typeof e)throw"no type was passed";var h={name:b,type:e,dependencies:f,hashCode:this.currentHashCode++};d&&(h.lifetime=d),g[b]=h},a.prototype.harness=function(a){var b=this;return function(c){return b.inject(a)(c)}},a.prototype.removeFake=function(a){delete this.fakes[a]},a.prototype.flushFakes=function(){this.fakes={}};var i=function(){var a=/^function ?\w* ?\(((?:\w+|(?:, ?))+)\)/,b=/, ?/;return function(c){var d,e=c.toString();return(d=a.exec(e))?d[1].split(b):null}}();return a.prototype.getType=function(a){var b=this.fakes[a]||this.types[a];return b?b.type:null},a.prototype.extend=function(a,b){var c=this.types[a];if(!c)throw'No type "'+a+'" found.';b.prototype=this.get(a)},a.prototype.noConflict=function(){window.injector=g},a.prototype.removeDefaultListener=function(){window.removeEventListener("hashchange",e)},a.prototype.clearState=function(){this.state={},_.each(this.types,function(a,b){"state"===a.lifetime&&delete this.cache[b]},this)},window.addEventListener("hashchange",e),new a}(); \ No newline at end of file diff --git a/package.json b/package.json index fbe161b..817c14b 100644 --- a/package.json +++ b/package.json @@ -1,54 +1,52 @@ -{ - "name": "injectjs", - "version": "1.0.0", - "description": "lightweight, small, high level dependency injector with support for multiple object lifetimes", - "main": "inject.js", - "scripts": { - "test": "karma karma.conf.js" - }, - "repository": { - "type": "git", - "url": "git@github.com:nstraub/injectjs.git" - }, - "keywords": [ - "dependency", - "injector", - "lightweight", - "inject", - "lifetime", - "singleton", - "transient", - "parent" - ], - "author": "Nicolas Straub", - "license": "ISC", - "bugs": { - "url": "https://github.com/nstraub/injectjs/issues" - }, - "homepage": "https://github.com/nstraub/injectjs", - "devDependencies": { - "karma": "~0.12.31", - "karma-script-launcher": "~0.1.0", - "karma-chrome-launcher": "^0.1.7", - "karma-coffee-preprocessor": "^0.2.1", - "karma-coverage": "^0.2.7", - "karma-firefox-launcher": "^0.1.4", - "karma-ie-launcher": "^0.1.5", - "karma-jasmine": "^0.3.5", - "karma-phantomjs-launcher": "^0.1.4", - "karma-story-reporter": "~0.2.2", - "grunt": "^0.4.5", - "grunt-karma": "~0.8.3", - "grunt-cli": "^0.1.13", - "grunt-contrib-concat": "^1.0.0", - "grunt-contrib-jshint": "~0.10.0", - "grunt-contrib-uglify": "~0.5.0", - "grunt-contrib-watch": "^0.6.1", - "bower": "^1.3.12", - "jasmine-core": "^2.2.0", - "karma-sauce-launcher": "~0.1.8" - }, - "dependencies": { - "lodash": "^3.5.0" - } -} +{ + "name": "injectjs", + "version": "v0.3.0", + "description": "lightweight, small, high level dependency injector with object lifetime management", + "main": "dist/inject.js", + "files": ["dist"], + "repository": { + "type": "git", + "url": "https://github.com/nstraub/injectjs.git" + }, + "keywords": [ + "dependency", + "injector", + "lightweight", + "inject", + "lifetime", + "singleton", + "transient", + "parent" + ], + "author": "Nicolas Straub", + "license": "ISC", + "bugs": { + "url": "https://github.com/nstraub/injectjs/issues" + }, + "homepage": "https://github.com/nstraub/injectjs", + "devDependencies": { + "bower": "^1.3.12", + "grunt": "^0.4.5", + "grunt-cli": "^0.1.13", + "grunt-contrib-concat": "^1.0.0", + "grunt-contrib-jshint": "~0.10.0", + "grunt-contrib-uglify": "~0.5.0", + "grunt-contrib-watch": "^0.6.1", + "grunt-karma": "~0.8.3", + "jasmine-core": "^2.2.0", + "karma": "~0.12.31", + "karma-chrome-launcher": "^0.1.7", + "karma-coffee-preprocessor": "^0.2.1", + "karma-coverage": "^0.2.7", + "karma-firefox-launcher": "^0.1.4", + "karma-ie-launcher": "^0.1.5", + "karma-jasmine": "^0.3.5", + "karma-phantomjs-launcher": "^0.1.4", + "karma-sauce-launcher": "~0.1.8", + "karma-script-launcher": "~0.1.0", + "karma-story-reporter": "~0.2.2" + }, + "dependencies": { + "lodash": "^3.5.0" + } +} diff --git a/src/inject.constructor.js b/src/inject.constructor.js index 6ed77fc..7b5a39c 100644 --- a/src/inject.constructor.js +++ b/src/inject.constructor.js @@ -5,3 +5,5 @@ function Injector() { this.fakes = {}; this.state = {}; } + +Injector.prototype.DEFAULT_LIFETIME = 'transient'; diff --git a/src/inject.js b/src/inject.js index cb7d29a..1951e5b 100644 --- a/src/inject.js +++ b/src/inject.js @@ -2,10 +2,26 @@ /* globals window: false */ /* exported lifetimes */ /* exported old_injector */ +/* globals get_dependency_names*/ + var lifetimes = ['singleton', 'transient', 'root', 'parent', 'state']; var old_injector = window.injector; +Injector.prototype.build_anonymous_descriptor = function (name) { // for when inject is called with an anonymous function + if (typeof name === 'function') { + return { + type: name, + dependencies: get_dependency_names(name) + }; + } else { + return { + type: name.pop(), + dependencies: name + }; + } +}; + /*---------------------- -- Injection Methods -- ----------------------*/ diff --git a/src/inject.registration.js b/src/inject.registration.js index 7500a7d..a156d14 100644 --- a/src/inject.registration.js +++ b/src/inject.registration.js @@ -3,29 +3,20 @@ */ /* globals Injector: false */ /* globals lifetimes: false */ +/* globals get_dependency_names*/ -var get_dependency_names = (function () { - var dependency_pattern = /^function ?\w* ?\(((?:\w+|(?:, ?))+)\)/; - var separatorPattern = /, ?/; - return function get_dependency_names(type) { - var serialized_type = type.toString(); - var serialized_dependencies; - - if (serialized_dependencies = dependency_pattern.exec(serialized_type)) { - return serialized_dependencies[1].split(separatorPattern); - } else { - return null; - } - }; -}()); +function assertLifetime (lifetime) { + if (!~lifetimes.indexOf(lifetime)) { + throw 'invalid lifetime "' + lifetime + '" provided. Valid lifetimes are singleton, transient, instance and parent'; + } +} +Injector.prototype.currentHashCode = 1; Injector.prototype.registerType = function (name, type, lifetime, provider) { - lifetime = lifetime || 'transient'; + lifetime = lifetime || this.DEFAULT_LIFETIME; - if (!~lifetimes.indexOf(lifetime)) { - throw 'invalid lifetime "' + lifetime + '" provided. Valid lifetimes are singleton, transient, instance and parent'; - } + assertLifetime(lifetime); this._register('types', name, type, lifetime); @@ -43,20 +34,18 @@ Injector.prototype.registerMain = function (main) { }; Injector.prototype.registerFake = function (name, type, lifetime) { - lifetime = lifetime || 'transient'; + lifetime = lifetime || this.DEFAULT_LIFETIME; - if (!~lifetimes.indexOf(lifetime)) { - throw 'invalid lifetime "' + lifetime + '" provided. Valid lifetimes are singleton, transient, instance and parent'; - } + assertLifetime(lifetime); this._register('fakes', name, type, lifetime); }; Injector.prototype._register = function (where, name, type, lifetime) { - var realType, dependencies; - var destination = this[where]; - if (typeof destination === 'undefined') { + var realType, dependencies, destination; + + if (typeof (destination = this[where]) === 'undefined') { throw 'invalid destination "' + where + '" provided. Valid destinations are types, providers, fakes and main'; } @@ -89,16 +78,3 @@ Injector.prototype._register = function (where, name, type, lifetime) { } destination[name] = result; }; -Injector.prototype.build_anonymous_descriptor = function (name) { // for when inject is called with an anonymous function - if (typeof name === 'function') { - return { - type: name, - dependencies: get_dependency_names(name) - }; - } else { - return { - type: name.pop(), - dependencies: name - }; - } -}; diff --git a/src/inject.util.js b/src/inject.util.js index ca7a7fb..a611dbb 100644 --- a/src/inject.util.js +++ b/src/inject.util.js @@ -2,6 +2,23 @@ /* globals injector: false */ /* globals old_injector: false */ /* globals window: false */ +/* exported get_dependency_names*/ + +var get_dependency_names = (function () { + var dependency_pattern = /^function ?\w* ?\(((?:\w+|(?:, ?))+)\)/; + var separatorPattern = /, ?/; + return function get_dependency_names(type) { + var serialized_type = type.toString(); + var serialized_dependencies; + + if (serialized_dependencies = dependency_pattern.exec(serialized_type)) { + return serialized_dependencies[1].split(separatorPattern); + } else { + return null; + } + }; +}()); + Injector.prototype.getType = function (name) { var type = this.fakes[name] || this.types[name]; diff --git a/src/require.shim.js b/src/require.shim.js index f99d7ba..1a2b0ae 100644 --- a/src/require.shim.js +++ b/src/require.shim.js @@ -2,12 +2,10 @@ * Created by nico on 17/03/2015. */ /* globals requirejs: false */ + requirejs.config({ shim: { - 'lodash': { - exports: '_' - }, - 'inject': { + 'injectjs': { deps: ['lodash'], exports: 'injector' }