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

dependence

v1.1.0

Published

Dependency injection brought to node.js. Powerful and easy-to-use.

Downloads

112

Readme

dependence NPM version Build Status Dependency Status

dependence brings dependency injection to node.js.

Manage your functions, variables and classes easily by turning them into dependencies. Get your modules under control.

Install

$ npm install dependence --save

Benefits

  • Faster code producing by simply require dependencies in the constuctor parameter list.
  • Better unit testing by easily mockup dependencies.

Example using dependence:

// Resolves the standard modules 'fs' and 'path' automatically
module.exports = function(fs, path) {
	return function(dirName, fileName) {
      return fs.readFileSync(path.join(dirName, fileName));
   }
};

Default way:

// No way to mock up those modules ...
var fs = require('fs');
var path = require('path');

module.exports = function(dirName, fileName) {
   return fs.readFileSync(path.join(dirName, fileName));
};

Usage

Shared dependency

var dependencies = require('dependence')();
var assert = require('assert');

dependencies.register('level', 1);

dependencies.register('nextLevel', function(level) {
   return level + 1 // level equals 1
});

dependencies.resolve(function(nextLevel) {
   assert.equal(nextLevel, 2);
});

Unshared dependency

var dependencies = require('dependence')();
var assert = require('assert');

var i = 0;
function counterDependecy() {
   return ++i;
}
counterDependecy.shared = false; // Default: true

dependencies.register('counter', counterDependecy);

dependencies.resolve(function(counter) {
   assert.equal(counter, 1);
});

dependencies.resolve(function(counter) {
   assert.equal(counter, 2); // would be 1 if 'shared' was true or undefined
});

Require explicitly

var dependencies = require('dependence')();
var assert = require('assert');

dependencies.register("Who is there?", "John");

function talkToJohn(whoIsThere) {
	assert.equal(whoIsThere, "John");
	done();
};

talkToJohn.require = ['Who is there?'];

dependencies.resolve(talkToJohn);

Options

  • shared (default: true) - Defines the default value for sharing dependencies. Each dependency can overwrite this value by setting its own 'shared' attriute.
  • node (default: true) - Set whether dependence should automatically try to require node modules if no dependency was found or not.
  • src (default: undefined) - Creates a dependency provider that returns dependencies of .js files on the fly.
var dependencies = require('dependence')({
	shared: false, // default: true
   node: true, // default: true
   src: __dirname + "/lib/**/*.js" // default: null
});

This equals shared: false ...

var dependencies = require('dependence')(false);

Following equals src: __dirname + "/lib/**/*.js" ...

var dependencies = require('dependence')(__dirname + "/lib/*.js");

If using src: lib/nextLevel.js

module.exports = function(level) {
	return level + 1;
};

You can also write this angular style:

module.exports = ['level', function(level) {
	return level + 1;
}];

License

MIT © Alexander Löhn