Comparing version 1.0.0 to 1.1.0
42
index.js
@@ -6,12 +6,12 @@ var args_list = require('args-list'); | ||
module.exports = function() { | ||
return caller = function(funcOrArray) { | ||
return resolve.call(caller, funcOrArray); | ||
return caller = function(func_or_array) { | ||
return resolve.call(caller, func_or_array); | ||
} | ||
}; | ||
function removeAfterFirstUndefined(values) { | ||
var firstUndefined = values.indexOf(undefined); | ||
function remove_after_first_undefined(values) { | ||
var first_undefined = values.indexOf(undefined); | ||
if(firstUndefined >= 0) { | ||
values = values.slice(0, firstUndefined); | ||
if(first_undefined >= 0) { | ||
values = values.slice(0, first_undefined); | ||
} | ||
@@ -21,7 +21,7 @@ return values; | ||
function getArgs(funcOrArray) { | ||
if(is_function(funcOrArray)) { | ||
function get_args(func_or_array) { | ||
if(is_function(func_or_array)) { | ||
return { | ||
args: args_list(funcOrArray), | ||
func: funcOrArray | ||
args: args_list(func_or_array), | ||
func: func_or_array | ||
}; | ||
@@ -31,4 +31,4 @@ } | ||
return { | ||
args: funcOrArray.slice(0, -1), | ||
func: funcOrArray.slice(-1)[0] | ||
args: func_or_array.slice(0, -1), | ||
func: func_or_array.slice(-1)[0] | ||
}; | ||
@@ -38,9 +38,9 @@ } | ||
function resolve(funcOrArray) { | ||
var argsAndFunc = getArgs(funcOrArray); | ||
var args = argsAndFunc.args; | ||
var func = argsAndFunc.func; | ||
function resolve(func_or_array) { | ||
var args_and_func = get_args(func_or_array); | ||
var args = args_and_func.args; | ||
var func = args_and_func.func; | ||
var that = this; | ||
var values = removeAfterFirstUndefined( | ||
var values = remove_after_first_undefined( | ||
args.map(function(arg) { | ||
@@ -55,5 +55,11 @@ var value = that[arg]; | ||
return function() { | ||
var resolved = function() { | ||
return func.apply(this, values.concat(to_array(arguments))); | ||
}; | ||
if(args.length === values.length) { | ||
resolved.all_arguments_resolved = true; | ||
} | ||
return resolved; | ||
} |
{ | ||
"name": "ddi", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "djanky dependency injection", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,3 +21,3 @@ ddi | ||
scope.register('foo', 'bar'); | ||
scope.foo = 'bar'; | ||
scope(function(foo) { | ||
@@ -27,3 +27,3 @@ console.log(foo) | ||
``` | ||
yields ``bar``. | ||
yields `bar`. | ||
@@ -42,2 +42,25 @@ You can also "nest" dependencies. | ||
``` | ||
yields ``bar pizza``. | ||
yields `bar pizza`. As well as use angular style dependency declaration. | ||
```javascript | ||
var create_scope = require('ddi'); | ||
var scope = create_scope(); | ||
scope.foo = 'bar'; | ||
scope(['foo', function(lol) { | ||
console.log(lol) | ||
})(); | ||
``` | ||
yields `bar`. Finally, `all_argments_resolved` will tell you whether or not | ||
more arguments need to be supplied to your function. | ||
```javascript | ||
var create_scope = require('ddi'); | ||
var scope = create_scope(); | ||
scope.foo = 'bar'; | ||
var resolved = scope(function(foo) { }); | ||
console.log(resolved.all_argments_resolved); | ||
``` | ||
yields `true`. |
16
test.js
@@ -35,1 +35,17 @@ var test = require('tape'); | ||
}); | ||
test('should have all_arguments_resolved be true', function(t) { | ||
var scope = create_scope(); | ||
scope.foo = 'bar'; | ||
var resolved = scope(function(foo) { }); | ||
t.ok(resolved.all_arguments_resolved, 'all arguments resolved'); | ||
t.end(); | ||
}); | ||
test('should have all_arguments_resolved be false', function(t) { | ||
var scope = create_scope(); | ||
scope.foo = 'bar'; | ||
var resolved = scope(function(foo, bar) { }); | ||
t.ok(!resolved.all_arguments_resolved, 'not all arguments resolved'); | ||
t.end(); | ||
}); |
6041
96
64