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

independable

v1.1.0

Published

Dead simple dependency container for node.js

Downloads

6

Readme

Independable

Dead simple dependency container for node.js and the browser.

Context

Independable was inspired by the dependable module, however I didn't like the way of defining dependencies, as it uses Function.toString() to identify the dependencies. Things will go wrong when you try to minify such files, and it simply isn't a "clean" solution.

Installation

npm install independable

or

bower install independable

Usage

You can retreive the container as

// Node & commonjs
var independable = require('independable');
var container = independable();

// AMD
define(['independable'], function(independable) {
    var container = independable();
});

// Global
var independable = window.independable;
var container = independable();

depending on your environment. Subsequently there are two ways to specify your dependencies. You could either register them, or define them.

Registering dependencies

The container.register() method expects you to specify a dependency directly. For example

// Registering a string dependency
container.register('some-string', 'a string value');

// Registering an object as dependency
var object = {};
container.register('some-object', object);

or equivalent

container.register({
  "some-string": "a string value",
  "some-object": {}
});

to register multiple dependencies at once.

Defining dependencies

If you want to only create objects on-the-fly as they are needed, you may provide a factory function using the container.define()method. For example

var Constructor = function() {
  this.foo = 'bar';
};
container.define('on-the-fly', function() {
  return new Constructor();
});

As such, new Constructor() is only called when requested by container.get('on-the-fly'). Note that if you use container.get('on-the-fly'); the constructor is not called again, but the object that was created earlier is returned. Also note that

container.register('dep', 'value');

and

container.define('dep', function() {
  return 'value';
});

are equivalent.

Relying on other dependencies

If a certain dependency depends on another dependency, there are two ways to handle this. Either

container.register('one', 1);
container.register('two', 2);
container.define('three', {
  deps: ['one', 'two'],
  get: function(one, two) {
    return one + two;
  }
});

or

container.register('one', 1);
container.register('two', 2);
container.define('three', function() {
  var one = this.get('one'),
      two = this.get('two');
  return one + two;
});

are equivalent. As can be seen, this in the factory function refers to the dependency container itself. As such, you can also pass it to functions depending on the container as

var needsTheContainer = function(container) {
  // Do something with the container
};
container.define('dependent', function() {
  return needsTheContainer(this);
});

Retreiving dependencies

var dep = container.get('dependency-id');

Since version 1.1.0 you can also directly access the dependencies which makes them suitable for object destructuring as well

var dep = container['dependency-id'];
var { dep } = container;

Deleting existing dependencies

Deleting dependencies can be useful to clear up some memory:

container.delete('dependency-id');

License

Copyright (c) 2016 Sebastiaan Marynissen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.