config.js
* @author Hermann Mayer <hermann.mayer92@gmail.com>
var extend = require('extend');
* @param {Object} options - Options of the config
var Config = function(options)
if ('undefined' === typeof options) {
throw new Error('No options defined');
this.path = options.path || null;
this.default = options.default || {};
this.values = options.values || {};
if (options.default && options.values) {
* Load a configuration from a given path.
* @param {String} [path] - Path to the config to load
Config.prototype.load = function(path)
throw new Error('No path was given or set');
throw new Error('Path "' + path + '" does not exists');
if (!fs.statSync(path).isFile()) {
throw new Error('Path "' + path + '" is not a file');
var extension = require('path').extname(path);
if ('.json' === extension || '.js' === extension) {
throw new Error('Failed to parse and load the config file');
* @param {String} [key] - Key to get or without an key get the whole config
Config.prototype.get = function(key)
return this.values[key] || null;
* Set config|config-key by given value.
* @param {String} [key] - Key to set or without an key overwrite the whole config
* @param {Mixed} value - Value to set
Config.prototype.set = function(key, value)
if ('undefined' === typeof value) {
throw new Error('Value was not specified');
// If we got a default config, merge the new over it
* Set the default configuration.
* @param {Object} config - Config to set as default
Config.prototype.setDefault = function(config)
throw new Error('Default config was not specified');
* Get the predefined default configuration.
Config.prototype.getDefault = function()
* Merge in a new config over the predefined default config.
* @param {Object} config - Config to merge in
* @param {Boolean} [deep] - Perform a deep merge
Config.prototype.merge = function(config, deep)
deep = ('undefined' === typeof deep) ? true : deep;