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

container.js

v1.0.15

Published

A dead-simple instance container for node.js.

Downloads

22

Readme

Container.js

A dead-simple instance container for node.js.

dependenciesBuild Status

Motivation

Container.js aims to be as simple and unobtrusive as possible while providing a reliable way to store and access instances of a predefined constructor. While it can be used in many ways , the main motivation behind the module was to provide a way to share constructor instances between a node.js application by attaching a container instance to the class/constructor.

Installation

npm install container.js

Usage

Instantiation

On it's simplest form

var Container = require("container.js");

var constructorContainer = new Container(Constructor);

However a most likely desired usage will be the following

var Container = require("container.js");

var Constructor = function(){

};

Constructor.container = new Container(Constructor);

module.exports = Constructor;

for if the Human constructor module is exported and then required elsewhere, the container and it's items will be available as

var Constructor = require('constructor');

Constructor.container...

Default arguments

If you would like to define default arguments for all constructed instances which can be overridden/extended, it is possible to do so in two ways.

Traditional

Let

var Human = function(gender,country,favouriteColor){
	this.gender = gender;
	this.country = country;
	this.favouriteColor = favouriteColor;
};

module.exports = Human;

If your constructor accept one or multiple arguments as the previously defined Human constructor and not a single configuration object you can define your container default arguments as

// Instantiate the container with the Human constructor.
var container = new Container(Human);
// Set the default arguments
container.defaults = ['male','USA','yellow'];

And then whenever you add a new instance to your container as

container.add('paul');

The Human constructor will now be called with the 'male','USA','yellow' arguments .

Want to override your defaults ?

Call the add method with the individual instance arguments as

container.add('sarah','female','canada','pink');

Want to override just some of your defaults?

Call the add method with the individual instance arguments and undefined to use the default value for a parameter

container.add('carl',undefined,undefined,'green');

The Human constructor will now be called with the 'male','USA','green' arguments (we previously defined 'male','USA' as defaults)

Single configuration object

Let

var Alien = function(configuration){
	this.specie = configuration.specie;
	this.planet = configuration.planet;
	this.language = configuration.language;
};

module.exports = Alien;

If your constructor accepts single configuration object, you can define your container default arguments as

// Instantiate the container with the Alien constructor.
var container = new Container(Alien);
// Set the default arguments
container.defaults = {
	specie : 'wookie',
	planet : 'kashyyyk',
	language : 'shyriiwook'
};

And then whenever you add a new instance to your container as

container.add('chewbacca');

The Alien constructor will now be called with the configuration object

{
	specie : 'wookie',
	planet : 'kashyyyk',
	language : 'shyriiwook'
}

Want to override your defaults ?

Call the add method with the individual instance configuration object attributes as

container.add('han solo',{
	specie : 'human',
	planet : 'corellia',
	language : 'english'
});

Want to override just some of your defaults?

Call the add method with the individual instance configuration argument attributes omitting the attributes where the default value should be used

container.add('wicket',{
	specie : 'ewok'
});

The Alien constructor will now be called with the configuration object (we previously defined the planet and language defaults)

{
	specie : 'ewok',
	planet : 'kashyyyk',
	language : 'shyriiwook'
}

Adding an instance

// Returns the added instance
container.add(identifier,args...);

Retrieving an instance

// Returns the retrieved instance.
container.get(identifier);

Removing an instance

container.remove(identifier);

Checking for existence

// Returns a boolean.
container.has(identifier); // Returns a boolean

Installation

Installing npm (node package manager)

  curl http://npmjs.org/install.sh | sh

Installing Container.js

  [sudo] npm install container.js

Run Tests

All of the Container.js tests are written in jasmine, and designed to be run with npm.

  $ npm install --dev
  $ npm test

Author: Joel Hernández