0.1.6 • Published 9 years ago

nsloader v0.1.6

Weekly downloads
11
License
MIT
Repository
github
Last release
9 years ago

Build Status Build Status npm version

NPM

nsLoader - Node.js namespace loader

nsloader is a small utility that allows using namespaces for requires.

Installation

$ npm install nsloader

Usage

var loader = require('nsloader');

loader.register('mymodule/*', function (ns) {
  var filename = path.join(__dirname, ns.substr('mymodule/'.length));
  return fs.existsSync(filename) ? filename : false;
});

Then the equivilant of:

var something = require('./lib/something');
var something = require('../../../lib/something');
var something = require('./something');

would be:

var something = loader.require('mymodule/lib/something');

Example

A real world example could be:

var path = require('path'),
    fs = require('fs'),
    loader = require('nsloader');

// Allow access to the mymodule index file.
loader.register('mymodule', function () {
  'use strict';

  return path.resolve(path.join(__dirname, '../index.js'));
});

// Allow access to anything after 'mymodule'.
loader.register('mymodule/*', function (ns) {
  'use strict';

  var filename = path.join(__dirname, ns.substring(7));
  if (fs.existsSync(filename + '.js')) {
    return filename + '.js';
  } else if (fs.existsSync(path.join(filename, 'index.js'))) {
    return path.join(filename, 'index.js');
  }

  return false;
});

Testing

A mocha test suite has been provided and can be run by:

$ npm test
0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago