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

di-modular

v1.0.4

Published

Small and versatile framework for developing scalable JavaScript applications.

Downloads

8

Readme

DiModular

Small and versatile framework for developing scalable JavaScript applications. DiModular is inspired by the Scalable JavaScript Application Architecture presented by by Nicholas Zakas, also known as the Core-Module-Sandbox pattern. However, instead of a "sandbox", a dependency injection container is used for handling dependencies and extending core functionality.

Installation

$ npm install di-modular

Api

diModular.register(dependency, value)

Registers value as named dependency.

diModular.register('$someDependency', 'someValue');

diModular.factory(dependency, factory)

Registers factory as named dependency. Factories can lists registered dependencies in their argument list, which will be injected when the factory is called.

diModular.factory('$someDependency', function($otherDependency) {
  return {
    someValue: $otherDependency
  };
});

Alternative syntax that is more resilient to code minifcation and name mangling:

diModular.factory('$someDependency', ['$otherDependency', function($otherDependency) {
  return {
    someValue: $otherDependency
  };
}]);

diModular.get(dependency)

Access named dependency.

diModular.module(name, moduleFactory)

Registers a named module factory. Module factories can lists registered dependencies in their argument list, which will be injected when the module is instantiated. Modules should implement an init method and a destroy method, which will be called as the module is started and stopped respectively.

diModular.module('someModule', function($someDependency) {
  return {
    init: function() {
      // Use $someDependency
    },

    destroy: function() {
      // ...
    }

  }
});

Alternative syntax that is more resilient to code minification and name mangling:

diModular.module('someModule', ['$someDependency', function($someDependency) {
  return {
    init: function() {
      // Use $someDependency
    },

    destroy: function() {
      // ...
    }

  }
}]);

diModular.start(name[, arg1][, arg2][, ...])

Starts named module. The module's init method is called. If provided, optional arguments are passed to the init method.

diModular.startAll([, arg1][, arg2][, ...])

Starts all non-running modules. Each started module's init method is called. If provided, optional arguments are passed to each init method.

diModular.stop(name)

Stops named module. The module's destroy method is called.

diModular.stopAll()

Stops all running modules. Each stoped module's destroy method is called.

Example

var EventEmitter = require('events').EventEmitter;
var DiModular = require('di-modular');

var modular = new DiModular();

// Register named value dependency
modular.register('$event', new EventEmitter());

// Register named factory dependency
modular.factory('$carFactory', function() {
  return {
    create: function(model) {
      return {
        model: model,
        speed: 10
      };
    }
  }
});

modular.module('honda', function($event, $carFactory) {
  return {
    init: function(opts) {
      this.sound = opts.sound;

      this.car = $carFactory.create('Honda');

      $event.emit('create', this.car);
      $event.on('honk', this.honk = this.honk.bind(this));
    },

    destroy: function() {
      // Do some cleanup, typically remove event listeners.
      $event.removeListener('honk', this.honk);

      $event.emit('destroy', this.car);
    },

    car: null,

    sound: '',

    honk: function() {
      console.log(this.car.model + ' says: "' + this.sound + '!"');
    }
  };
});

// Access the event emitter and listen for `create` event.
modular.get('$event').on('create', function(car) {
  console.log(car.model + ' has been created');
});

// Access the event emitter and listen for `destroy` event.
modular.get('$event').on('destroy', function(car) {
  console.log(car.model + ' has been destroy');
});

// Start module and pass in some options
modular.start('honda', {
  sound: 'Honk, honk'
});

// Access the event emitter and emit `honk` event.
modular.get('$event').emit('honk');

// Stop module
modular.stop('honda');

// Output:
// > honda has been created
// > honda says: "Honk honk!"
// > honda has been destroy

Test

Run unit tests;

$ npm test

Create test coverage report:

$ npm run-script test-cov

License

MIT