Default options for NodeJS, NPM, and CommonJS modules
Defaultable is a simple drop-in tool to make your Node API very convenient for your users. It comes from internal Iris Couch tooling.
Defaultable is pure CommonJS Javascript, and is also available as an NPM module.
$ npm install defaultable
Is it any good?
Yes.
What your users see
With Defaultable, these are the promises you make to your users (in documentation, presentations, etc.)
Dear users, just require my code and use it like normal.
var api = require("my_mod");
api.do_stuff("Bob", { minimum:5, dollars:10 });
api.do_stuff("Eve", { minimum:5, dollars:800});
If you are using the same options a lot, set them as defaults.
var api = require("my_mod").defaults({ "minimum": 5 });
api.do_stuff("Bob", { dollars:10 });
api.do_stuff("Eve", { dollars:800});
Defaults can even inherit from other defaults.
var api = require("my_mod");
var fivers = api.defaults({ "minimum": 5}});
var rich = fivers.defaults({"dollars": 10});
var poor = fivers.defaults({"dollars": 800});
poor.do_stuff("Bob");
rich.do_stuff("Eve");
What you see
Defaulable wraps a CommonJS module.
Your original code:
var DEFAULTS = { "minimum":0, "dollars":0 };
exports.do_stuff = function(person, opts) {
opts = opts || {};
console.log("Processing: " + person);
console.log(" minimum = " + opts.minimum || DEFAULTS.minimum);
console.log(" dollars = $" + opts.dollars || DEFAULTS.dollars);
}
Your new code:
require('defaultable')(module,
{ "minimum": 0
, "dollars": 0
}, function(module, exports, DEFAULTS) {
exports.do_stuff = function(person, opts) {
opts = opts || {};
console.log("Processing: " + person);
console.log(" minimum = " + opts.minimum || DEFAULTS.minimum);
console.log(" dollars = $" + opts.dollars || DEFAULTS.dollars);
}
})
How it works
It's really simple.
Defaultable passes the initial defaults to you as DEFAULTS
. Use module
, module.exports
, or exports
as usual to build your module API.
Your API gets an additional .defaults()
function, which will re-evaluate your code with new user-provided defaults.
Automatic defaults in require()
If you have multiple related modules, it can be nice for them to share defaults.
require('defaultable')(module,
{ "minimum": 0
, "dollars": 0
}, function(module, exports, DEFS) {
var submod = require('./sub_mod').defaults(DEFS);
})
For this situation, defaultable provides a wrapped require()
function. It works just like before, however if the modules you load is itself defaultable, it will be initialized with the current defaults.
require('defaultable')(module,
{ "minimum": 0
, "dollars": 0
}, function(module, exports, DEFS, require) {
var submod = require('./sub_mod');
var legacy_mod = require('./legacy_mod');
var http = require('http');
})
If you do not want your module to inherit anything implicitly, use Defaultable's .def()
function instead. This is useful for top-level modules of packages, for example.
require('defaultable').def(module,
{ "minimum": 0
, "dollars": 0
}, function(module, exports, DEFS) {
exports.check = function() {
console.log("Dollars = " + dollars);
}
})