Skip to content

Commit

Permalink
restructuring and first functional Grunt config
Browse files Browse the repository at this point in the history
  • Loading branch information
nstraub committed Mar 12, 2016
1 parent 987527f commit 5c18b67
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 150 deletions.
5 changes: 1 addition & 4 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
},
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -130,7 +130,7 @@ So let's complicate things a bit...

# <a name="road"></a>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)
Expand All @@ -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
Expand All @@ -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**
Expand Down Expand Up @@ -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**
Expand All @@ -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**
Expand Down
2 changes: 1 addition & 1 deletion Tests/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
},

Expand Down
2 changes: 1 addition & 1 deletion Tests/karma.dist.min.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "injectjs",
"version": "0.0.0",
"version": "0.3.0",
"authors": [
"Nicolas Straub"
],
Expand Down
89 changes: 50 additions & 39 deletions dist/inject.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 --
----------------------*/
Expand Down Expand Up @@ -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);

Expand All @@ -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';
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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];

Expand Down
4 changes: 2 additions & 2 deletions dist/inject.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 52 additions & 54 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]: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"
}
}
Loading

0 comments on commit 5c18b67

Please sign in to comment.