-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (134 loc) · 3.98 KB
/
index.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* A Config Formatting
*/
var _ = require('underscore');
var Config = function() {
this.DEFAULT_VALUE = '';
}
/**
* Recursively copies all properties from source into destination - overwriting
* and adding as necessary.
* @param destination {object}
* @param source {object}
* @return {void}
*/
Config.prototype.deepCopy = function(destination, source, seen) {
seen = seen || [];
seen.push(source);
for (var p in source) {
if (typeof source[p] === 'object' && !(source[p] instanceof Array)) {
if (!destination.hasOwnProperty(p)) {
destination[p] = {};
}
if (seen.indexOf(source[p]) === -1) { //have not yet traversed to source[p]
this.deepCopy(destination[p], source[p], seen);
}
} else {
destination[p] = source[p];
}
}
}
/**
* Loads a config file or multiple files
* @param configFiles {string|array}
* @return {void}
*/
Config.prototype.loadConfig = function(configFiles) {
if (typeof configFiles === 'string') {
configFiles = [configFiles];
}
var config = {};
for (var index in configFiles) {
config = _.extend(config, this.loadSingleConfigFile(configFiles[index]));
}
for(var key in config) {
this.set(key, config[key]);
}
}
/**
* Loads a given configuration file and returns an object
* representing that file
* @param filename {string} the file to load
*/
Config.prototype.loadSingleConfigFile = function(filename) {
try {
return require(filename);
} catch(e) {
console.log('There was an error loading file: ' + filename, e);
}
return {};
}
/**
* Connects the database, should be done in the app.js file
* @param databaseSetupFunction {function} a database setup that returns
* a connection or accepts a callback parameter
*/
Config.prototype.connectDatabase = function(databaseSetup) {
var callback = _.bind(function(error, database) {
if (database) {
this.set('db', database);
}
}, this);
callback(null, _.bind(databaseSetup, this)(callback));
}
/**
* This allows you to drill into the object safely,
* use a dot to drill into the next object
* @since 0.0.1
* @access public
* @returns object
*/
Config.prototype.get = function(key, defaultValue) {
defaultValue = typeof defaultValue != 'undefined' ? defaultValue : this.DEFAULT_VALUE;
if (!this.isKeyFullString(key)) {
console.log('Cannot get value. "key" must be of type string');
return defaultValue;
}
return _.reduce(key.split("."), function(result, partOfKey) {
if (result[partOfKey]) {
return result[partOfKey];
} else {
return defaultValue;
}
}, this);
}
/**
* This can set variables inside the config object
* @todo implement the extend parameter
* @since 0.0.1
* @access public
* returns void
*/
Config.prototype.set = function(key, value) {
if (!this.isKeyFullString(key)) {
console.log('Cannot set value. "key" must be of type string');
return;
}
var keyParts = key.split(".");
var baseKey = keyParts.pop();
var parentDataObject = _.reduce(keyParts, function(result, partOfKey) {
if (typeof result[partOfKey] == 'undefined') {
result[partOfKey] = {};
}
return result[partOfKey];
}, this);
if (typeof parentDataObject[baseKey] === 'undefined') {
parentDataObject[baseKey] = {};
}
if (typeof value === 'object' && !(value instanceof Array)) {
this.deepCopy(parentDataObject[baseKey], value);
} else {
parentDataObject[baseKey] = value;
}
}
/**
* Determines if a key is both a string and not empty
* @since 0.0.5
* @param key {mixed}
* @return boolean true if the string is not empty; false otherwise
*/
Config.prototype.isKeyFullString = function(key) {
return typeof key == 'string' && '' !== key;
}
/* Creates a singleton instance */
module.exports = new Config();