@knowark/injectarkjs
Advanced tools
Comparing version 0.5.0 to 0.6.0
// Base Factory Interface | ||
export class Factory { | ||
/** @param {Object} config */ | ||
constructor(config = {}) { | ||
this.config = config | ||
} | ||
/** @param {string} method @return {function} */ | ||
@@ -4,0 +9,0 @@ extract (method) { |
@@ -13,2 +13,6 @@ import { Factory } from './factory.js' // eslint-disable-line | ||
get config () { | ||
return this.factory.config | ||
} | ||
/** @param {string} resource */ | ||
@@ -58,10 +62,8 @@ resolve (resource) { | ||
let extract = (method) => this.factory[method] | ||
if (this.factory.extract) { | ||
extract = this.factory.extract.bind(this.factory) | ||
} | ||
let extract = this.factory.extract.bind(this.factory) | ||
const builder = extract(rule.method) | ||
if (builder) { | ||
const dependencies = builder.dependencies || [] | ||
const dependencies = ( | ||
builder.dependencies || this._parseDependencies(builder)) | ||
const dependencyInstances = [] | ||
@@ -89,2 +91,13 @@ | ||
} | ||
_parseDependencies (builder) { | ||
const stripComments = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; | ||
const argumentNames = /([^\s,]+)/g; | ||
const functionString = builder.toString().replace(stripComments, '') | ||
const result = functionString.slice(functionString.indexOf( | ||
'(') + 1, functionString.indexOf(')')).match(argumentNames) || [] | ||
return result.map(dependency => | ||
dependency.charAt(0).toUpperCase() + dependency.slice(1)) | ||
} | ||
} |
{ | ||
"name": "@knowark/injectarkjs", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Dependency Injector for Javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -7,3 +7,3 @@ import { Factory } from '../lib/factory.js' | ||
factory = new Factory() | ||
factory.factoryMethod = function () { | ||
factory.factoryMethod = () => { | ||
return 'ResolvedResource' | ||
@@ -13,14 +13,12 @@ } | ||
// it('has an abstract extract method', function () { | ||
// try { | ||
// factory.extract('factoryMethod') | ||
// } catch (error) { | ||
// expect(error).toBeTruthy() | ||
// } | ||
// }) | ||
it('has a default extract method', function () { | ||
it('has a default extract method', () => { | ||
const method = factory.extract('factoryMethod') | ||
expect(method()).toEqual('ResolvedResource') | ||
}) | ||
it('can be instantiated with a config object', () => { | ||
const config = { key: 'value' } | ||
factory = new Factory(config) | ||
expect(factory.config).toBe(config) | ||
}) | ||
}) |
import { Injectark } from '../lib/injectark.js' | ||
import { Factory } from '../lib/factory.js' | ||
@@ -23,4 +24,5 @@ class A { } | ||
class StandardFactory { | ||
constructor () { | ||
class StandardFactory extends Factory { | ||
constructor (config) { | ||
super(config) | ||
this._standardC.dependencies = ['A', 'B'] | ||
@@ -52,6 +54,6 @@ this._standardD.dependencies = ['B', 'C'] | ||
class DefaultFactory { | ||
constructor () { | ||
class DefaultFactory extends Factory { | ||
constructor (config) { | ||
super(config) | ||
this.c.dependencies = ['A', 'B'] | ||
this.d.dependencies = ['B', 'C'] | ||
} | ||
@@ -197,4 +199,5 @@ | ||
class CoreFactory { | ||
constructor () { | ||
class CoreFactory extends Factory { | ||
constructor (config) { | ||
super(config) | ||
this._coreY.dependencies = ['X'] | ||
@@ -297,3 +300,4 @@ } | ||
const parent = new Injectark() | ||
const factory = new DefaultFactory() | ||
const config = { key: 'value' } | ||
const factory = new DefaultFactory(config) | ||
let injector = null | ||
@@ -314,2 +318,6 @@ | ||
it('provides its factory config through a config property', () => { | ||
expect(injector.config).toBe(injector.factory.config) | ||
}) | ||
describe('Injectark default factory resolvers', function () { | ||
@@ -322,3 +330,9 @@ it('resolves a resource by its camelCase name by default', function () { | ||
expect(Object.keys(injector.registry).length).toEqual(2) | ||
instance = injector.resolve('C') | ||
expect(instance).toEqual(jasmine.any(C)) | ||
instance = injector.resolve('D') | ||
expect(instance).toEqual(jasmine.any(D)) | ||
expect(Object.keys(injector.registry).length).toEqual(4) | ||
}) | ||
@@ -329,4 +343,4 @@ | ||
injector.resolve('DataService') | ||
} catch (e) { | ||
expect(e.message).toEqual('Not implemented service.') | ||
} catch (error) { | ||
expect(error.message).toEqual('Not implemented service.') | ||
} | ||
@@ -333,0 +347,0 @@ }) |
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
14173
397