-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
151 lines (120 loc) · 3 KB
/
index.js
File metadata and controls
151 lines (120 loc) · 3 KB
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
/**
* Squid Config
*
* A lightweight configuration class for Squid applications
*
* Get/Set methods are inspired by backbone-nested.js
* https://github.com/afeld/backbone-nested
*
*/
'use strict';
var _ = require('lodash')
var _LOGNAME = '[Config] '
// Initialize Config Class
//
// @params {mixed} object or file path
// @params {mixed} object or file path
// @return {object} Config instance
//
function Config( defaults, extend, envName )
{
// Constants
// -------------
// test instance uniqueness
this._UID = _.uniqueId('config_')
this._DEFAULTENV = 'default'
// CORE's settings
// ------------------------
// default app configuration
var confDefault = this.getConfigParam( defaults )
, confExtend = ( !_.isUndefined( extend ) )
? this.getConfigParam( extend )
: {}
this._ENV = envName || this._DEFAULTENV
this._CONFIG = _.merge(
{}
, confDefault
, confExtend
)
return this
}
Config.prototype.getConfigParam = function( arg )
{
if( _.isString( arg ) )
{
try
{
return require( arg )
}
catch( e ) { throw new Error( _LOGNAME + 'can not find config file ' + arg ) }
}
if( _.isObject( arg ) )
return arg
throw new Error( _LOGNAME + 'argument must be a valid file path or an object')
}
// Get Config environment
//
// @return {string}
//
Config.prototype.getEnv = function()
{
return this._ENV
}
// Get Config item
//
// @params {string} dot notation based key's path
// @params {string} default value if not found
// @return {mixed}
//
Config.prototype.get = function( path, defaults )
{
// no path asked,
// return the whole config
if( _.isUndefined( path ) )
return this._CONFIG
var fields = path.split('.')
, result = this._CONFIG
for( var i = -1, n = fields.length; ++i < n; )
{
result = result[ fields[ i ] ]
if( result == 'undefined' && i < n - 1 )
result = {}
if( typeof result === 'undefined' )
return result || defaults
}
return result
}
// Set a config value
//
// @params {string} dot notation based key's path
// @params {string} value to set
// @params {object} unset key
// @return {object} Config instance
//
Config.prototype.set = function( path, val, options )
{
options = options || {}
var fields = path.split('.')
, result = this._CONFIG
for( var i = -1, n = fields.length; ++i < n && result !== undefined; )
{
var field = fields[ i ]
// If the last in the path, set the value
if( i === n - 1 )
{
options.unset ? delete result[ field ] : result[ field ] = val
}
else
{
// Create the child object if it doesn't exist, or isn't an object
if( typeof result[ field ] === 'undefined' || ! _.isObject( result[ field ] ) )
{
result[ field ] = {}
}
// Move onto the next part of the path
result = result[ field ]
}
}
return this
}
module.exports = Config