Comparing version 0.0.5 to 0.0.6
{ | ||
"name": "fount", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "A source from which dependencies flow", | ||
@@ -33,4 +33,5 @@ "main": "./src/index.js", | ||
"gulp-mocha": "^0.5.1", | ||
"should": "^4.0.4" | ||
"should": "^4.0.4", | ||
"sinon": "^1.12.1" | ||
} | ||
} |
@@ -86,4 +86,9 @@ # Fount | ||
### function | ||
Registering a function with fount will cause it to invoke the function during resolution and return the result. | ||
Registering a function with fount will cause it to invoke the function during resolution and return the result with two exceptions: | ||
1. The function is a stub or some other abstraction that cannot have dependencies resolved for it | ||
2. The function has dependencies which do not exist for fount | ||
In these exceptional scenarios, fount will resolve the dependency with the function itself rather than calling it for you. | ||
```javascript | ||
@@ -90,0 +95,0 @@ fount.register( 'factory', function() { return 'a thing!' } ); |
var should = require( 'should' ); | ||
var when = require( 'when' ); | ||
var fount = require( '../src/index.js' ); | ||
var sinon = require( 'sinon' ); | ||
@@ -22,2 +23,38 @@ describe( 'when resolving values', function() { | ||
describe( 'when resolving sinon.stub', function() { | ||
var stub = sinon.stub(); | ||
var result; | ||
before( function( done ) { | ||
fount.register( 'aStub', stub ); | ||
fount.inject( function( aStub ) { | ||
result = aStub; | ||
done(); | ||
} ); | ||
} ); | ||
it( 'should resolve the stub as a function', function() { | ||
result.should.eql( stub ); | ||
} ); | ||
} ); | ||
describe( 'when resolving a function with unresolvable dependencies', function() { | ||
var fn = function( x, y, z ) { | ||
return x + y + z; | ||
}; | ||
var result; | ||
before( function( done ) { | ||
fount.register( 'unresolvable', fn ); | ||
fount.inject( function( unresolvable ) { | ||
result = unresolvable( 1, 2, 3 ); | ||
done(); | ||
} ); | ||
} ); | ||
it( 'should resolve the stub as a function', function() { | ||
result.should.eql( 6 ); | ||
} ); | ||
} ); | ||
describe( 'when resolving functions', function() { | ||
@@ -24,0 +61,0 @@ |
@@ -10,5 +10,10 @@ var _ = require( 'lodash' ); | ||
function checkDependencies( fn, dependencies ) { | ||
return ( _.isFunction( fn ) && !dependencies.length ) ? | ||
trim( /[(]([^)]*)[)]/.exec( fn.toString() )[ 1 ].split( ',' ) ) : | ||
dependencies; | ||
var fnString = fn.toString(); | ||
if( /[(][^)]*[)]/.test( fnString ) ) { | ||
return ( _.isFunction( fn ) && !dependencies.length ) ? | ||
trim( /[(]([^)]*)[)]/.exec( fnString )[ 1 ].split( ',' ) ) : | ||
dependencies; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
@@ -79,2 +84,29 @@ | ||
function canResolve( containerName, dependencies, scopeName ) { | ||
scopeName = scopeName || 'default'; | ||
return _.all( dependencies, function( key ) { | ||
if( _.isArray( key ) ) { | ||
var ctr = container( containerName ); | ||
var vals = []; | ||
key.forEach( function( k ) { | ||
var originalKey = k; | ||
var parts = k.split( '.' ); | ||
if( parts.length > 1 ) { | ||
ctr = container( parts[ 0 ] ); | ||
k = parts[ 1 ]; | ||
} | ||
vals.push( ctr[ k ] ); | ||
} ); | ||
return _.all( vals ); | ||
} else { | ||
var parts = key.split( '.' ); | ||
if( parts.length > 1 ) { | ||
containerName = parts[ 0 ]; | ||
key = parts[ 1 ]; | ||
} | ||
return container( containerName )[ key ]; | ||
} | ||
} ); | ||
} | ||
function resolve( containerName, key, scopeName ) { | ||
@@ -122,3 +154,3 @@ scopeName = scopeName || 'default'; | ||
return function( scopeName ) { | ||
if( _.isFunction( value ) ) { | ||
if( _.isFunction( value ) && dependencies && canResolve( containerName, dependencies, scopeName ) ) { | ||
var args = dependencies.map( function( key ) { | ||
@@ -145,3 +177,3 @@ return resolve( containerName, key, scopeName ); | ||
} | ||
else if( _.isFunction( value ) ) { | ||
else if( _.isFunction( value ) && dependencies && canResolve( containerName, dependencies, scopeName ) ) { | ||
var args = dependencies.map( function( key ) { | ||
@@ -165,3 +197,3 @@ return resolve( containerName, key, scopeName ); | ||
var promise; | ||
if( _.isFunction( value ) ) { | ||
if( _.isFunction( value ) && dependencies && canResolve( containerName, dependencies ) ) { | ||
var args = dependencies.map( function( key ) { | ||
@@ -168,0 +200,0 @@ return resolve( containerName, key ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25851
8
576
163
4