npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

samurai-inject

v1.0.3

Published

Simple Ioc Container

Downloads

28

Readme

Samurai Inject

A Simple IOC Container

Coverage Status JavaScript Style Guide npm

Installation

Install with npm

npm install samurai-inject --save

How to use

/*
* Get container factory function
*/
const { Container } =  require('samurai-inject')

// Create the instance of the container
const  container  =  Container() 

class Foo {
  print () {
   console.log('Foo')
  }
}

class Bar {
  print () {
   console.log('Bar')
  }
}

// We can register classes or functions
container.register('foo', Foo)
container.register('bar', Bar)

// And then we can resolve those classes and get the instances
const foo = constainer.resolve('foo')
const bar = constainer.resolve('bar')

foo.print() // Output: Foo
bar.print() // Output: Bar

Class vs Factories

We can use classes or factories patterns

const { Container } =  require('samurai-inject')
const  container  =  Container() 

const Baz = () => {
  const print = () => {
    console.log('Baz')
  }
  return {
    print
  }
}

class Qux {
  print () {
   console.log('Qux')
  }
}

// We can register classes or functions
container.register('qux', Qux)
container.register('Baz', Baz)

// And then we can resolve those classes and get the instances
const qux = constainer.resolve('qux')
const baz = constainer.resolve('baz')

qux.print() // Output: Qux
baz.print() // Output: Baz

Resolve parameters as dependencies (default behavior)

The default behavior to solve the parameters which are other objects is passing the data as a single parameter object.

const { Container } =  require('samurai-inject')
const  container  =  Container() 

class Corge {
  // The default behavior pass an object to the constructor
  // So we can destructure the object and get the param than we want
  constructor ({
    grault
  }) {
    this.grault = grault
  }
  print () {
    console.log('Corge')
    this.grault.print()
  }
}

class Grault {
  print () {
    console.log('Grault')
  }
}

// We can register classes or functions
// In this case we specify the dependencies in the third parameter
container.register('corge', Corge, ['grault'])
container.register('grault', Grault)

// And then we can resolve those classes and get the instances
const corge = constainer.resolve('corge')
const grault = constainer.resolve('grault')

corge.print() // Output: Corge Grault
grault.print() // Output: Grault

Resolve parameters individually

We can override the default behavior to solve the parameters one by one sending it separately as a classical pattern used by the begin of the times.

const { Container, PARAMS_MODE } =  require('samurai-inject')
const  container  =  Container() 

class Garply {
  constructor (waldo) {
    this.waldo = waldo
  }
  print () {
    console.log('Garply')
    this.waldo.print()
  }
}
class Waldo {
  print () {
    console.log('Waldo')
  }
}

// We can register classes or functions
// In this case we specify the dependencies in the third parameter
// In this case we specify that the parameter are gonna pass it individually
container.register('garply', Garply, ['waldo'], PARAMS_MODE.asIndividualParams)
container.register('waldo', Waldo)

// And then we can resolve those classes and get the instances
const garply = constainer.resolve('garply')
const waldo = constainer.resolve('waldo')

garply.print() // Output: Garply Waldo
waldo.print() // Output: Waldo

Static params

Sometimes we want to pass some fixed/static parameters to our objects like a connection strings or something like that.

const { Container } =  require('samurai-inject')
const  container  =  Container() 

class Fred {
  constructor ({
    plugh,
    connectionString
  }) {
    this.plugh = plugh
    this.connectionString = connectionString
  }
  connect () {
    console.log('Connection to ' + this.connectionString)
    console.log('Limit of rows is ' + this.plugh.getLimitOfRows)
  }
}
class Plugh {
  constructor ({
    limitOfRows
  }){
    this.limitOfRows = limitOfRows
  }
  getLimitOfRows () {
    return this.limitsOfRows
  }
}

// We can register classes or functions
// In this case we specify the dependencies in the third parameter
// In this case we can specify the static parameter using an object instead an string
container.register('fred', Fred, ['plugh',
  { connectionString: 'mongo:mylocalmongo:2727/testdb' }
])
container.register('plugh', Plugh, [
  { limitsOfRows: 100 }
])

// And then we can resolve those classes and get the instances
const fred = constainer.resolve('fred')
const plugh = constainer.resolve('plugh')

fred.print() 
// Output: Connection to mongo:mylocalmongo:2727/testdb
// Output: Limit of rows is 100
plugh.getLimitOfRows() // Output: 100