Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@knowark/injectarkjs

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@knowark/injectarkjs - npm Package Compare versions

Comparing version 0.8.3 to 0.8.4

3

lib/index.d.ts

@@ -20,6 +20,9 @@ export declare class Factory {

get<Type>(resource: new () => Type): Type
get(resource: string): unknown
set<Type>(resource: new () => Type, instance: Type): void
set(resource: string, instance: unknown): void
resolve<Type>(resource: new () => Type, strict?: boolean): Type
resolve(resource: string, strict?: boolean): unknown

@@ -26,0 +29,0 @@

10

lib/injectark.js

@@ -18,4 +18,5 @@ import { Factory } from './factory.js' // eslint-disable-line

/** @param {string} resource */
/** @param {string | object} resource */
get (resource) {
if (typeof resource !== 'string') resource = resource.name
const allowed = this.factory.allowed

@@ -28,9 +29,12 @@ if (allowed?.length && !allowed.includes(resource)) {

/** @param {string} resource @param {object} instance */
/** @param {string | object} resource @param {object} instance */
set (resource, instance) {
if (typeof resource !== 'string') resource = resource.name
this.registry[resource] = instance
}
/** @param {string} resource @param {boolean=} strict */
/** @param {string | object} resource @param {boolean=} strict */
resolve (resource, strict = false) {
if (typeof resource !== 'string') resource = resource.name
const fetched = this._registryFetch(resource)

@@ -37,0 +41,0 @@ if (fetched) {

@@ -96,21 +96,21 @@ import { Injectark } from './injectark.js'

describe('Injectark default', function () {
describe('Injectark default', () => {
let injector = null
beforeEach(function () {
beforeEach(() => {
injector = new Injectark()
})
it('can be instantiated', function () {
it('can be instantiated', () => {
expect(injector).toBeTruthy()
})
it('has a null parent', function () {
it('has a null parent', () => {
expect(injector.parent).toBeNull()
})
it('has an empty registry', function () {
it('has an empty registry', () => {
expect(injector.registry).toEqual({})
})
it('has an empty strategy object', function () {
it('has an empty strategy object', () => {
expect(injector.strategy).toEqual({})

@@ -120,3 +120,3 @@ })

describe('Injectark params', function () {
describe('Injectark params', () => {
const parent = new Injectark()

@@ -127,3 +127,3 @@ const factory = new StandardFactory()

beforeEach(function () {
beforeEach(() => {
injector = new Injectark({

@@ -136,3 +136,3 @@ strategy,

it('has the given objects as attributes', function () {
it('has the given objects as attributes', () => {
expect(injector.parent).toBe(parent)

@@ -143,4 +143,4 @@ expect(injector.strategy).toBe(strategy)

describe('Injectark resolve', function () {
it('resolves a resource with no dependencies', function () {
describe('Injectark resolve', () => {
it('resolves a resource with no dependencies', () => {
let instance = injector.resolve('A')

@@ -154,40 +154,50 @@ expect(instance).toEqual(expect.any(A))

it('serves an already instantiated resource from its registry',
function () {
const instance = injector.resolve('A')
expect(instance).toEqual(expect.any(A))
const registryInstance = injector.resolve('A')
expect(registryInstance).toBe(instance)
it('serves an already instantiated resource from its registry', () => {
const instance = injector.resolve('A')
expect(instance).toEqual(expect.any(A))
const registryInstance = injector.resolve('A')
expect(registryInstance).toBe(instance)
expect(Object.keys(injector.registry).length).toEqual(1)
})
expect(Object.keys(injector.registry).length).toEqual(1)
})
it("generates and injects a resource's dependencies on resolve",
function () {
const instance = injector.resolve('C')
expect(instance).toEqual(expect.any(C))
expect(instance.a).toEqual(expect.any(A))
expect(instance.b).toEqual(expect.any(B))
expect(Object.keys(injector.registry).length).toEqual(3)
expect(injector.resolve('A')).toBe(instance.a)
expect(injector.resolve('B')).toBe(instance.b)
})
it("doesn't persist ephemeral dependencies on the registry",
function () {
injector.strategy = {
A: {
method: 'standardA',
ephemeral: true
}
it("generates and injects a resource's dependencies on resolve", () => {
const instance = injector.resolve('C')
expect(instance).toEqual(expect.any(C))
expect(instance.a).toEqual(expect.any(A))
expect(instance.b).toEqual(expect.any(B))
expect(Object.keys(injector.registry).length).toEqual(3)
expect(injector.resolve('A')).toBe(instance.a)
expect(injector.resolve('B')).toBe(instance.b)
})
it('serves a resource given its constructor class', () => {
const instance = injector.resolve(C)
expect(instance).toEqual(expect.any(C))
expect(instance.a).toEqual(expect.any(A))
expect(instance.b).toEqual(expect.any(B))
expect(Object.keys(injector.registry).length).toEqual(3)
expect(injector.resolve('A')).toBe(instance.a)
expect(injector.resolve('B')).toBe(instance.b)
})
it("doesn't persist ephemeral dependencies on the registry", () => {
injector.strategy = {
A: {
method: 'standardA',
ephemeral: true
}
const instance = injector.resolve('A')
}
const instance = injector.resolve('A')
expect(instance).toEqual(expect.any(A))
expect(Object.keys(injector.registry).length).toEqual(0)
})
expect(instance).toEqual(expect.any(A))
expect(Object.keys(injector.registry).length).toEqual(0)
})
})
})
describe('Injectark forge', function () {
describe('Injectark forge', () => {
class X {

@@ -237,4 +247,4 @@

describe('Injectark hierarchical resolve', function () {
beforeEach(function () {
describe('Injectark hierarchical resolve', () => {
beforeEach(() => {
factory = new StandardFactory()

@@ -256,3 +266,3 @@ strategy = Object.assign({}, standardStrategy)

it('forges a new injector from a parent one', function () {
it('forges a new injector from a parent one', () => {
expect(injector.parent).toBe(parent)

@@ -263,3 +273,3 @@ expect(injector.strategy).toBe(strategy)

it('resolves a resource own by its parent registry', function () {
it('resolves a resource own by its parent registry', () => {
const instance = injector.resolve('X')

@@ -273,3 +283,3 @@ expect(instance).toEqual(expect.any(X))

it('resolves a resource its parent know how to build', function () {
it('resolves a resource its parent know how to build', () => {
const instance = injector.resolve('Y')

@@ -281,3 +291,3 @@ expect(instance).toEqual(injector.parent.registry.Y)

it('returns a unique resource if "unique" is true', function () {
it('returns a unique resource if "unique" is true', () => {
injector.strategy.Y = {

@@ -291,3 +301,3 @@ method: 'coreY',

it('might forge a subinjector without arguments', function () {
it('might forge a subinjector without arguments', () => {
const subInjector = injector.forge()

@@ -299,3 +309,3 @@ expect(subInjector.parent).toBe(injector)

it('resolves from its own registry if there is no parent', function () {
it('resolves from its own registry if there is no parent', () => {
injector.parent = null

@@ -308,3 +318,3 @@ const instance = injector.resolve('Y')

describe('Injectark optional strategy', function () {
describe('Injectark optional strategy', () => {
const parent = new Injectark()

@@ -315,3 +325,3 @@ const config = { key: 'value' }

beforeEach(function () {
beforeEach(() => {
injector = new Injectark({

@@ -323,3 +333,3 @@ factory,

it('has a strategy that defaults to an empty object', function () {
it('has a strategy that defaults to an empty object', () => {
expect(injector.parent).toBe(parent)

@@ -330,3 +340,3 @@ expect(injector.factory).toBe(factory)

it('provides its factory config through a config property', () => {
it('allows setting a custom resource instance given its name', () => {
class CustomA {}

@@ -342,2 +352,13 @@ const originalA = injector.resolve('A')

it('allows setting a custom resource instance given its class', () => {
class CustomA {}
const originalA = injector.resolve(A)
expect(originalA instanceof A).toBeTruthy()
injector.set(A, new CustomA())
const customA = injector.resolve(A)
expect(customA instanceof CustomA).toBeTruthy()
})
it('allows to manually replace a dependency using "set" method', () => {

@@ -347,4 +368,4 @@ expect(injector.config).toBe(injector.factory.config)

describe('Injectark default factory resolvers', function () {
it('resolves a resource by its camelCase name by default', function () {
describe('Injectark default factory resolvers', () => {
it('resolves a resource by its camelCase name by default', () => {
let instance = injector.resolve('A')

@@ -364,3 +385,3 @@ expect(instance).toEqual(expect.any(A))

it('resolves TitleCase to camelCase by default', function () {
it('resolves TitleCase to camelCase by default', () => {
try {

@@ -375,3 +396,3 @@ injector.resolve('DataService')

describe('Injectark strict and restricted access', function () {
describe('Injectark strict and restricted access', () => {
const config = { key: 'value' }

@@ -381,3 +402,3 @@ const factory = new DefaultFactory(config)

beforeEach(function () {
beforeEach(() => {
injector = new Injectark({

@@ -402,8 +423,8 @@ factory

let instance = injector.get('C')
let instance = injector.get(C)
expect(instance instanceof C).toBeTruthy()
instance = injector.get('D')
instance = injector.get(D)
expect(instance instanceof D).toBeTruthy()
})
})
{
"name": "@knowark/injectarkjs",
"version": "0.8.3",
"version": "0.8.4",
"description": "Dependency Injector for Javascript",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc