@knowark/injectarkjs
Advanced tools
Comparing version 0.10.2 to 0.10.3
@@ -9,8 +9,14 @@ export class Builder { | ||
const name = config.factory || this.base | ||
const names = (name || Object.keys( | ||
const [pivot, ...overrides] = (name || Object.keys( | ||
this.factories).shift()).split(',').map(name => name.trim()) | ||
const Constructor = this.factories[names.slice().pop()] | ||
return names.reduce((store, item) => Object.assign( | ||
store, item), new Constructor(config)) | ||
const Constructor = this.factories[pivot] | ||
const factory = new Constructor(config) | ||
for (const override of overrides) { | ||
const prototype = this.factories[override].prototype | ||
for (const property of Object.getOwnPropertyNames(prototype)) { | ||
factory[property] = prototype[property] | ||
} | ||
} | ||
return factory | ||
} | ||
} |
@@ -8,5 +8,12 @@ import { describe, it, expect } from '@jest/globals' | ||
class AlphaFactory extends Factory {} | ||
class BetaFactory extends Factory {} | ||
class GammaFactory extends Factory {} | ||
class AlphaFactory extends Factory { | ||
alphaMethod () { return 'ALPHA:alpha' } | ||
} | ||
class BetaFactory extends AlphaFactory { | ||
betaMethod () { return 'BETA:beta' } | ||
} | ||
class GammaFactory extends BetaFactory { | ||
alphaMethod () { return 'GAMMA:alpha' } | ||
gammaMethod () { return 'GAMMA:gamma' } | ||
} | ||
@@ -50,17 +57,36 @@ const factories = { | ||
let factory = builder.build({ | ||
factory: 'beta,alpha' | ||
}) | ||
let factory = builder.build({ factory: 'beta,alpha' }) | ||
expect(factory).toBeInstanceOf(BetaFactory) | ||
expect(factory.alphaMethod()).toEqual('ALPHA:alpha') | ||
expect(factory.betaMethod()).toEqual('BETA:beta') | ||
expect(() => factory.gammaMethod()).toThrow() | ||
factory = builder.build({ factory: 'alpha,beta' }) | ||
expect(factory).toBeInstanceOf(AlphaFactory) | ||
expect(factory.alphaMethod()).toEqual('ALPHA:alpha') | ||
expect(factory.betaMethod()).toEqual('BETA:beta') | ||
expect(() => factory.gammaMethod()).toThrow() | ||
factory = builder.build({ | ||
factory: 'alpha,beta' | ||
}) | ||
factory = builder.build({ factory: 'alpha, beta, gamma' }) | ||
expect(factory).toBeInstanceOf(AlphaFactory) | ||
expect(factory.alphaMethod()).toEqual('GAMMA:alpha') | ||
expect(factory.betaMethod()).toEqual('BETA:beta') | ||
expect(factory.gammaMethod()).toEqual('GAMMA:gamma') | ||
factory = builder.build({ factory: 'gamma, alpha, beta' }) | ||
expect(factory).toBeInstanceOf(AlphaFactory) | ||
expect(factory).toBeInstanceOf(BetaFactory) | ||
expect(factory).toBeInstanceOf(GammaFactory) | ||
expect(factory.alphaMethod()).toEqual('ALPHA:alpha') | ||
expect(factory.betaMethod()).toEqual('BETA:beta') | ||
expect(factory.gammaMethod()).toEqual('GAMMA:gamma') | ||
factory = builder.build({ | ||
factory: 'alpha, beta, gamma' | ||
}) | ||
factory = builder.build({ factory: 'gamma, beta, alpha' }) | ||
expect(factory).toBeInstanceOf(AlphaFactory) | ||
expect(factory).toBeInstanceOf(BetaFactory) | ||
expect(factory).toBeInstanceOf(GammaFactory) | ||
expect(factory.alphaMethod()).toEqual('ALPHA:alpha') | ||
expect(factory.betaMethod()).toEqual('BETA:beta') | ||
expect(factory.gammaMethod()).toEqual('GAMMA:gamma') | ||
}) | ||
}) |
{ | ||
"name": "@knowark/injectarkjs", | ||
"version": "0.10.2", | ||
"version": "0.10.3", | ||
"description": "Dependency Injector for Javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
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
22668
621