-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
108 lines (101 loc) · 4.13 KB
/
options.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
/*
* options.js
*
* Set and get configuration options. Set defaults, use environment variables or override from an object.
*
* Usage:
* const Options = require('./src/options.js')
* let opts = new Options
* opts.setOpts(objWithOptions) // set key-value pair for each k-v pair in objWithOptions
* opts.setFromEnv(arrayOfEnvVarKeys) // set k-v pairs using array of keys and their associated environment variables values
* opts.getOpt(optName [,optType]) // get value for key optName as type optType
* opts.getAll() // get all k-v pairs
*
* Examples:
* let opts = new Options
* let a = opts.getOpt("theKey","string","The default value")
* let b = opts.getOpt("theKey")
*
* v 1.1.0 2017-12-14 jdr - added default values for getOpt
* v 1.0.0 2017-12-06 jdr - initial version
*/
function Options() {
// opts - the internal object to use for setting/getting options (key+value pairs)
this.opts = {}
// setOpts(valsObject) - set the options using the keys+values from the given object
this.setOpts = (valsObject) => {
if ( typeof(valsObject) == "object" ) {
let keys=Object.keys(valsObject)
for (let i=0; i < keys.length; i++) {
this.opts[keys[i]] = valsObject[keys[i]]
}
} else {
// TODO: ponder - what is the right thing to do?
return false
}
return true
}
// getOpt(keyName [, keyType]) - get the value for the keyName, convert it to keyType if given (default "string")
this.getOpt = (keyName, keyType="string", defaultVal="") => {
let ret = ""
switch (keyType) {
case "string":
ret = (typeof(this.opts[keyName]) != "undefined") ? this.opts[keyName] : defaultVal
break;
case "int":
// round to nearest integer
ret = Math.round(Number((typeof(this.opts[keyName]) != "undefined") ? this.opts[keyName] : defaultVal))
break;
case "number":
ret = Number((typeof(this.opts[keyName]) != "undefined") ? this.opts[keyName] : defaultVal )
break;
case "flag":
// if the flag key exists, mark it as true, regardless of the value
ret = (typeof(this.opts[keyName]) != "undefined")
break;
case "boolean":
if (typeof(this.opts[keyName]) != "undefined") {
ret = Boolean(this.opts[keyName])
} else {
ret = false || Boolean(defaultVal) // note that this will be true for any non-empty string, even "false"
}
break;
default:
}
return ret
}
// Include specific functions for each return type
this.getString= (keyName,defaultVal="") => {
return this.getOpt(keyName,"string",defaultVal)
}
this.getInt= (keyName,defaultVal="") => {
return this.getOpt(keyName,"int",defaultVal)
}
this.getNumber= (keyName,defaultVal="") => {
return this.getOpt(keyName,"number",defaultVal)
}
this.getFlag= (keyName,defaultVal="") => {
return this.getOpt(keyName,"flag",defaultVal)
}
this.getBoolean= (keyName,defaultVal="") => {
return this.getOpt(keyName,"boolena",defaultVal)
}
// setFromEnv(envArray) - use envArray for the key names to pull in environment variables
this.setFromEnv = (envArray) => {
let ret = {}
// Verify argArray is actually a non-empty array
if (typeof(envArray) == "object" && Array.isArray(envArray) && envArray.length > 0 ) {
for (let i=0; i< envArray.length; i++) {
if (typeof(process.env[envArray[i]]) == "string") {
ret[envArray[i]] = process.env[envArray[i]]
}
}
}
return this.setOpts(ret)
}
// getAll() - return the full object of keys+values
this.getAll = () => {
return this.opts
}
}
module.exports = Options