configuration-master
Advanced tools
Comparing version 0.0.3 to 0.1.0
121
index.js
const _ = require('lodash'); | ||
// private variables | ||
const myConfig = {}; | ||
const COMMON_KEY = "__COMMON"; | ||
const myMap = { | ||
"process.stdout": process.stdout, | ||
"process.stderr": process.stderr | ||
} | ||
function _processConfig(data) | ||
class Config | ||
{ | ||
_.forEach(data, (value, key, collection) => { | ||
if (_.isObject(value)) | ||
constructor() | ||
{ | ||
// private variables | ||
const COMMON_KEY = "__COMMON"; | ||
const myMap = { | ||
"process.stdout": process.stdout, | ||
"process.stderr": process.stderr | ||
} | ||
const _myConfig = {}; | ||
const _processConfig = (data) => | ||
{ | ||
collection[key] = _processConfig(value); | ||
_.forEach(data, (value, key, collection) => { | ||
if (_.isObject(value)) | ||
{ | ||
collection[key] = _processConfig(value); | ||
} | ||
else if(_.isString(value)) | ||
{ | ||
if ((value.substring(0,2) == "${") && (value.substring(value.length-1) == "}")) | ||
{ | ||
collection[key] = _mapper(value.substring(2, value.length-1)); | ||
} | ||
} | ||
}); | ||
return data; | ||
} | ||
else if(_.isString(value)) | ||
const _mapper = (value) => | ||
{ | ||
if ((value.substring(0,2) == "${") && (value.substring(value.length-1) == "}")) | ||
let retVal = myMap[value]; | ||
if ((retVal == null) && (value.indexOf("process.env") == 0)) | ||
{ | ||
collection[key] = _mapper(value.substring(2, value.length-1)); | ||
retVal = process.env[value.substring(12)]; | ||
} | ||
return retVal; | ||
} | ||
}); | ||
return data; | ||
} | ||
this.setConfig = function(jsonObject, namespace) | ||
{ | ||
jsonObject = _processConfig(jsonObject); | ||
_myConfig[namespace || COMMON_KEY] = _.merge(_myConfig[namespace || COMMON_KEY] || {}, jsonObject); | ||
} | ||
function _mapper(value) | ||
{ | ||
let retVal = myMap[value]; | ||
this.getValue = (key, namespace) => | ||
{ | ||
return (_myConfig[namespace || COMMON_KEY] == null)?null:_myConfig[namespace || COMMON_KEY][key]; | ||
} | ||
if ((retVal == null) && (value.indexOf("process.env") == 0)) | ||
{ | ||
retVal = process.env[value.substring(12)]; | ||
} | ||
return retVal | ||
} | ||
this.getNamespace = (namespace) => | ||
{ | ||
if (Object.keys(_myConfig).includes(namespace)) | ||
{ | ||
return _myConfig[namespace]; | ||
} | ||
return _myConfig[COMMON_KEY][namespace]; | ||
} | ||
function config() | ||
{ | ||
this.direct = myConfig; | ||
this.setValue = (key, value, namespace) => | ||
{ | ||
_myConfig[namespace || COMMON_KEY][key] = value; | ||
} | ||
this.loadConfig = function(filename, namespace) | ||
{ | ||
let tmp = require(filename); | ||
this.setConfig(tmp, namespace); | ||
this.loadConfig = (filename, namespace) => | ||
{ | ||
let tmp = require(filename); | ||
this.setConfig(tmp, namespace); | ||
} | ||
} | ||
this.setConfig = function(jsonObject, namespace) | ||
{ | ||
jsonObject = _processConfig(jsonObject); | ||
myConfig[namespace || COMMON_KEY] = _.merge(myConfig[namespace || COMMON_KEY] || {}, jsonObject); | ||
} | ||
this.getValue = function(key, namespace) | ||
{ | ||
} | ||
const tmp = new Config(); | ||
const config = new Proxy(tmp, { | ||
get: function(target, name, receiver) { | ||
if (Object.keys(target).includes(name)) | ||
{ | ||
return target[name]; | ||
} | ||
return (myConfig[namespace || COMMON_KEY] == null)?null:myConfig[namespace || COMMON_KEY][key]; | ||
} | ||
return target.getNamespace(name); | ||
} | ||
}); | ||
this.setValue = function(key, value, namespace) | ||
{ | ||
myConfig[namespace || COMMON_KEY] = value; | ||
} | ||
} | ||
module.exports = new config(); | ||
module.exports = config; |
{ | ||
"name": "configuration-master", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Simple configuration load/access tool.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,7 @@ ## Configuration Master | ||
#### Release Notes | ||
v0.1.0 | ||
- breaks .direct. functionality from earlier versions. | ||
- can now access properties directly. | ||
#### Installation | ||
@@ -45,3 +50,3 @@ | ||
>```js | ||
>config.getValue("settingsA"); //returns 'value A' | ||
>config.getValue("settingA"); //returns 'value A' | ||
>``` | ||
@@ -51,3 +56,3 @@ >or | ||
>```js | ||
>config.getValue('settingsA', 'namespace'); //returns 'value A' | ||
>config.getValue('settingA', 'namespace'); //returns 'value A' | ||
>``` | ||
@@ -57,3 +62,3 @@ >or | ||
>```js | ||
>config.direct.namespace.settingsA //returns 'value A' | ||
>config.namespace.settingsA //returns 'value A' | ||
>``` |
@@ -28,3 +28,3 @@ const configMgr = require('./'); | ||
console.log(configMgr.direct.namespace.fire); | ||
console.log(configMgr.namespace.fire); | ||
require('./test2Config'); |
const configMgr = require('./'); | ||
console.log(configMgr.getValue("earth", "namespace")); | ||
console.log(configMgr.direct.__COMMON.silver); | ||
console.log(configMgr.silver); | ||
configMgr.silver = 9 | ||
console.log(configMgr.silver); |
4044
104
60
5