-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added .toFunction() and Grunt task runner
- Loading branch information
Showing
9 changed files
with
273 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
|
||
.DS_Store | ||
|
||
node_modules | ||
*.espressostorage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
module.exports = function(grunt) { | ||
|
||
grunt.initConfig({ | ||
pkg: grunt.file.readJSON('package.json'), | ||
jshint: { | ||
gruntfile: { | ||
src: 'Gruntfile.js', | ||
options: { | ||
node: true | ||
} | ||
}, | ||
subsumer: { | ||
src: [ | ||
'src/subsumer.js' | ||
] | ||
} | ||
}, | ||
uglify: { | ||
options: { | ||
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' + | ||
'<%= grunt.template.today("yyyy-mm-dd") %> - ' + | ||
'http://torjue.mit-license.org */\n' | ||
}, | ||
build: { | ||
files: { | ||
'build/subsumer.min.js': ['src/subsumer.js'] | ||
} | ||
} | ||
}, | ||
concat: { | ||
options: { | ||
stripBanners: true, | ||
banner: '<%= uglify.options.banner %>' | ||
}, | ||
dist: { | ||
src: ['src/subsumer.js'], | ||
dest: 'build/subsumer.js' | ||
} | ||
} | ||
}); | ||
|
||
grunt.loadNpmTasks('grunt-contrib-jshint'); | ||
grunt.loadNpmTasks('grunt-contrib-uglify'); | ||
grunt.loadNpmTasks('grunt-contrib-concat'); | ||
|
||
grunt.registerTask('default', ['jshint', 'uglify', 'concat']); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/*! Subsumer.js - v0.5.0 - 2013-07-08 - http://torjue.mit-license.org */ | ||
(function(undefined) { | ||
"use strict"; | ||
|
||
var ioc = function(){ | ||
|
||
var bindings = {}, | ||
originalBindings = {}, | ||
getParamNames, | ||
getDependenciesFor, | ||
createInstance, | ||
isBound, | ||
resolve, | ||
afterUse, | ||
instantiate, | ||
use, | ||
bind; | ||
|
||
getParamNames = function(fn){ | ||
var fnString = fn.toString(); | ||
return fnString.match(/\(.*?\)/)[0] | ||
.replace(/[()]/gi,'') | ||
.replace(/\s/gi,'') | ||
.split(','); | ||
}; | ||
|
||
getDependenciesFor = function(fn){ | ||
var params = fn.$inject === undefined ? getParamNames(fn): fn.$inject; | ||
var args = [null]; | ||
for (var i=0; i<params.length; i++) { | ||
var match = resolve(params[i]); | ||
if (!!match) { | ||
args.push(match); | ||
} | ||
else { | ||
args.push(undefined); | ||
} | ||
} | ||
return args; | ||
}; | ||
|
||
createInstance = function(fn){ | ||
var args = getDependenciesFor(fn); | ||
return new (Function.prototype.bind.apply(fn, args))(); | ||
}; | ||
|
||
isBound = function(key) { | ||
return key in bindings; | ||
}; | ||
|
||
resolve = function(key){ | ||
var val = bindings[key]; | ||
if(typeof val === 'function'){ | ||
return createInstance(val); | ||
} | ||
else { | ||
return val; | ||
} | ||
}; | ||
|
||
afterUse = function(fn, key){ | ||
var returnValue = fn(key); | ||
for(var k in originalBindings){ | ||
if (originalBindings.hasOwnProperty(k)) { | ||
bindings[k] = originalBindings[k]; | ||
} | ||
} | ||
originalBindings = {}; | ||
return returnValue; | ||
}; | ||
|
||
instantiate = function(fn){ | ||
if(typeof fn === 'function'){ | ||
return createInstance(fn); | ||
} | ||
else { | ||
return fn; | ||
} | ||
}; | ||
|
||
use = function(key, value){ | ||
originalBindings[key] = bindings[key]; | ||
bindings[key] = value; | ||
return { | ||
resolve: function(key){ return afterUse(resolve, key); }, | ||
instantiate: function(fn){ return afterUse(instantiate, fn); }, | ||
use: use | ||
}; | ||
}; | ||
|
||
bind = function(key){ | ||
return { | ||
to: function(value){ | ||
bindings[key] = value; | ||
return { | ||
asSingleton: function(){ | ||
bindings[key] = createInstance(value); | ||
}, | ||
asFunction: function(){ | ||
bindings[key] = function(){ return value; }; | ||
} | ||
}; | ||
}, | ||
toSingleton: function(singleton){ | ||
bindings[key] = createInstance(singleton); | ||
}, | ||
toFunction: function(fn){ | ||
bindings[key] = function(){ return fn; }; | ||
} | ||
}; | ||
}; | ||
|
||
return { | ||
resolve: resolve, | ||
bind: bind, | ||
isBound: isBound, | ||
use: use, | ||
instantiate: instantiate | ||
}; | ||
}; | ||
|
||
/* global exports:true, window:true, module:true, define:true */ | ||
// register for AMD module | ||
if (typeof define === 'function' && define.amd) { | ||
define("subsumer", ioc); | ||
} | ||
|
||
// export for node.js | ||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports = module.exports = ioc; | ||
} | ||
exports = ioc; | ||
} | ||
|
||
// browser | ||
if (typeof window !== 'undefined') { | ||
window.subsumer = ioc; | ||
} | ||
/* global exports:false, window:false, module:false, define:false */ | ||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "Subsumer.js", | ||
"version": "0.5.0", | ||
"description": "Lightweight IoC for JavaScript applications", | ||
"main": "Subsumer.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/torjue/Subsumer.js.git" | ||
}, | ||
"keywords": [ | ||
"ioc", | ||
"di" | ||
], | ||
"author": "Torjus Eidet", | ||
"license": "MIT", | ||
"readmeFilename": "readme.md", | ||
"bugs": { | ||
"url": "https://github.com/torjue/Subsumer.js/issues" | ||
}, | ||
"devDependencies": { | ||
"grunt": "~0.4.1", | ||
"grunt-contrib-jshint": "~0.6.0", | ||
"grunt-contrib-uglify": "~0.2.2", | ||
"grunt-contrib-concat": "~0.3.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.