all files / config/ index.js

76.36% Statements 42/55
50% Branches 11/22
75% Functions 9/12
71.05% Lines 27/38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137                                                                                                                                                                                                                           
'use strict';
 
Object.defineProperty(exports, "__esModule", {
	value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _path = require('path');
 
var _path2 = _interopRequireDefault(_path);
 
var _fs = require('fs');
 
var _fs2 = _interopRequireDefault(_fs);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
/**
 * Config Class
 */
 
var Config = function () {
	function Config() {
		_classCallCheck(this, Config);
 
		this._config = {};
	}
 
	/**
  * Internal Config Storage
  * @type {Object}
  */
 
 
	_createClass(Config, [{
		key: 'get',
 
 
		/**
   * Retrieve a config value via dot-notation
   * 
   * @param  {String} key dot-notation representation of config path
   * @param  {Mixed}  defaultVal the default value to be used if no config param is set at this location
   * @return {Mixed}
   */
		value: function get(key, defaultVal) {
			var i = void 0,
			    keys = key.split('.'),
			    curs = this._config;
 
			for (i = 0; i < keys.length; i++) {
				curs = curs[keys[i]];
			}
 
			return curs || defaultVal;
		}
 
		/**
   * Set a config value via dot-notation
   * 
   * @param {String} key dot-notation representatino of config path
   * @param {Config} val this class
   */
 
	}, {
		key: 'set',
		value: function set(key, val) {
			var i = void 0,
			    keys = key.split('.'),
			    curs = this._config;
 
			for (i = 0; i < keys.length - 1; i++) {
				var _key = keys[i];
 
				Eif (!curs[_key]) {
					curs[_key] = {};
				}
 
				curs = curs[_key];
			}
 
			curs[keys[keys.length - 1]] = val;
 
			return this;
		}
 
		/**
   * Load configs from a given folder
   * using their filenames as a key
   * 
   * @param  {String} __dir path to directory
   * @return {Config}
   */
 
	}, {
		key: 'loadFromFolder',
		value: function loadFromFolder(__dir) {
			var _this = this;
 
			var filenames = _fs2.default.readdirSync(__dir);
			filenames.forEach(function (file) {
				var name = void 0,
				    contents = void 0;
 
				name = file.replace('.jsx', '').replace('.js', '');
 
				if (name !== 'index') {
					contents = require(_path2.default.join(__dir, file));
					_this.set(name, contents.default || contents);
				}
			});
 
			return this;
		}
 
		/**
   * Load configs from a raw object
   * @param  {Object} obj configuration object
   * @return {Config}
   */
 
	}, {
		key: 'loadFromObj',
		value: function loadFromObj(obj) {
			this._config = obj;
 
			return this;
		}
	}]);
 
	return Config;
}();
 
exports.default = new Config();