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

administer

v0.0.8

Published

A common-sense, minimalist DI/IoC framework.

Downloads

24

Readme

Administer: Minimalist DI

npm administer Codeship Code Climate Test Coverage

Administer is a minimalist dependency injection framework for javascript projects that fully supports stamps (and ES6 classes and plain javascript functions). Check out the below example to quickly see how it's used, and then read the full API docs.

Yet Another DI Framework?

In short, yes. After trying many components, @joshdmiller couldn't find anything that precisely met his requirements, and so created Administer.

Specifically, Administer has the following design goals:

  • Be lightweight and minimalist with as few as possible dependencies.
  • Provide a little bit of magic for stamps, but don't shove it down anyone's throat.
  • Allow swapping components at runtime to allow easy mocking.
  • Use factories instead of frail string names to declare dependencies.
  • Don't require manually registering components with the injector.
  • Coming soon. Work in Node and the browser.

Example

Just install administer via npm:

$ npm install --save administer

And you're good to go! Now let's write some code. Assuming we have components A and B:

// a.js
import stampit from 'stampit';
import B from './b';

const A = stampit()
.static({ $inject: [ B ] })
.init( ({ instance, args }) => {
  let [ instance.b ] = args;
})
.methods({
  doSomething () {
    return this.b.doIt();
  }
});

export default A;

// b.js
import stampit from 'stampit';

const B = stampit()
.methods({
  doIt () {
    console.log( 'Hello!' );
  }
});

export default B;

You can get a new injector by calling Administer() and then fetching the component you want:

import A from './a';
import Administer from 'administer';

const adm = Administer({/* config */});
adm.get( A ).then( a => {
  a.doSomething();

  // and B is accessible on a because of the `init` above:
  a.b.doIt();
});

Using dependency injection is just that simple! But let's say you have unit tests of A that you want to test in isolation - that is to say, without B. You can simple define a mock, tell the injector to use it with a call to provide(), and then test your component:

import A from './a';
import B from './b';

const mockB = {
  doIt () {
    console.log( 'mock!' );
  }
})
;

const adm = Administer();
adm.provide( B, mockB );
adm.get( A )
.then( function ( a ) {
  a.doSomething();
  // -> 'mock!'
});

This is the core of the dependency injection framework. You create factories for the components, using either StampIt, as above, or ES6 classes or plain javascript functions. Then you specify dependencies straight away using an $inject property on the component factory. Finally, you ask the injector for an instance of the component you need and it will recursively resolve all dependencies, ensuring all components who request a particular component get precisely the same instance.

Check out the API documentation for more information. And feel free to contribute!