-
Alvaro Cabrera authoredAlvaro Cabrera authored
Container.js 2.08 KiB
"use strict";
var RandExp = require("randexp");
// set maximum default, see #193
RandExp.prototype.max = 10;
/**
* Container is used to wrap external libraries (faker, chance, randexp) that are used among the whole codebase. These
* libraries might be configured, customized, etc. and each internal JSF module needs to access those instances instead
* of pure npm module instances. This class supports consistent access to these instances.
*/
var Container = (function () {
function Container() {
// static requires - handle both initial dependency load (deps will be available
// among other modules) as well as they will be included by browserify AST
this.registry = {
faker: null,
chance: null,
// randexp is required for "pattern" values
randexp: RandExp
};
}
/**
* Override dependency given by name
* @param name
* @param callback
*/
Container.prototype.extend = function (name, callback) {
if (typeof this.registry[name] === 'undefined') {
throw new ReferenceError('"' + name + '" dependency is not allowed.');
}
this.registry[name] = callback(this.registry[name]);
};
/**
* Returns dependency given by name
* @param name
* @returns {Dependency}
*/
Container.prototype.get = function (name) {
if (typeof this.registry[name] === 'undefined') {
throw new ReferenceError('"' + name + '" dependency doesn\'t exist.');
}
else if (name === 'randexp') {
return this.registry['randexp'].randexp;
}
return this.registry[name];
};
/**
* Returns all dependencies
*
* @returns {Registry}
*/
Container.prototype.getAll = function () {
return {
faker: this.get('faker'),
chance: this.get('chance'),
randexp: this.get('randexp')
};
};
return Container;
}());
// TODO move instantiation somewhere else (out from class file)
// instantiate
var container = new Container();
module.exports = container;