UNPKG

srvoa

Version:

Infrastructure for service oriented architecture.

177 lines (148 loc) 4.93 kB
/** * srvoa - soa infrastructure for node js * * @copyright Copyright (c) 2015, Alrik Zachert * @license https://gitlab.com/kermit-js/kermit/blob/support/srvoa/LICENSE BSD-2-Clause */ "use strict"; /** * The config class with support for merging multiple hashes recursively and * retrieving a nested key by its dot separated string representation. */ 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; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } var Config = (function () { /** * The config hash. * * @private * @property _configHash {Object} */ /** * Initialize the private config hash property. * * @constructor */ function Config() { _classCallCheck(this, Config); this._configHash = {}; } /** * Sets and merges multiple config sets. * * @param {...Object} config * @return {ConfigService} */ _createClass(Config, [{ key: 'setConfig', value: function setConfig(config) { var l = arguments.length; if (l === 1) { this._configHash = arguments[0]; } else { this._configHash = {}; for (var i = 0; i < l; ++i) { this._mergeConfig(this._configHash, arguments[i]); } } return this; } /** * Merge the given config into the private config hash. * * @param {Object} config * @returns {Config} */ }, { key: 'mergeConfig', value: function mergeConfig(config) { this._mergeConfig(this._configHash, config); return this; } /** * @param {String} key * @param {*} defaultValue * @returns {*} */ }, { key: 'get', value: function get(key, defaultValue) { var parts = key.split('.'), l = parts.length, i = 0, scopeKey, scope = this._configHash; try { for (; i < l; ++i) { scopeKey = parts[i]; if (scopeKey in scope) { scope = scope[scopeKey]; } else { return defaultValue; } } return scope; } catch (e) { return defaultValue; } } /** * Returns the private config hash. * * @returns {Object} */ }, { key: 'getHash', value: function getHash() { return this._configHash; } /** * Calculate the type of a value - used by recursive merge logic. * * @private * @param value * @returns {string} */ }, { key: '_getConfigType', value: function _getConfigType(value) { var type = typeof value; if (type === 'object') { if (Array.isArray(value)) { type = 'array'; } } return type; } /** * Recursively merge object src into dest. Array values won't be merged but overridden. * * @param dest * @param src * @returns {*} */ }, { key: '_mergeConfig', value: function _mergeConfig(dest, src) { for (var property in src) { var value = src[property]; if (!(property in dest)) { dest[property] = value; } else if (value === null) { delete dest[property]; } else { var destType = this._getConfigType(dest[property]); var srcType = this._getConfigType(value); if (destType === 'object' && srcType === 'object') { this._mergeConfig(dest[property], value); } else { dest[property] = value; } } } return dest; } }]); return Config; })(); module.exports = Config;