Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Last active August 29, 2015 14:00
Show Gist options
  • Save dfkaye/11094033 to your computer and use it in GitHub Desktop.
Save dfkaye/11094033 to your computer and use it in GitHub Desktop.
monadic namespace loader sketch...
var registry = {};
function define(id) {
function define(param) {
var self = define.namespace, type = typeof param;
// handle invalid type? then...
type == 'function' && exec(param, self);
return type == 'string' && string(param, define);
}
// resolve id filename (); then registry, attach and away...
registry[id] || (registry[id] = new namespace(id));
define.namespace = registry[id];
return define;
}
function error(message) {
throw new Error(message);
}
function namespace(filename) {
this.id = filename;
this.module = module.parent;
this.filename = filename
this.__filename = __filename;
this.__dirname = __dirname;
this.require = parentRequire;
}
function parentRequire(path) {
return module.parent.require(path);
}
parentRequire.cache = module.constructor._cache;
function string(path, define) {
var self = define.namespace;
// parse, load, and away...
return define;
}
function exec(fn, namespace) {
// guard clauses
namespace.module || error('module is not defined');
make(fn, namespace)(namespace); // or f = make(fn, namespace); f(namespace);
return namespace.module.exports
}
function make(fn, namespace) {
// make context from namespace; fn toString;
return new function
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment