lib/index.js   A
last analyzed

Size

Lines of Code 107

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
nc 1
dl 0
loc 107
rs 10
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ ??? 0 3 1
1
/**
2
 * This class is responsible for 12-factor config parsing.
3
 */
4
class ConfigParser {
5
6
  /**
7
   * Class constructor.
8
   *
9
   * @param {Object} settings Parser settings.
10
   * @param {String} settings.prefix Prefix to prepend to each ENV variable.
11
   */
12
  constructor(settings = {}) {
13
    this.prefix = settings.prefix || '';
14
  }
15
16
  /**
17
   * Creates a new configuration entry and returns it's value.
18
   *
19
   * @param {String} name - Name of the ENV variable.
20
   * @param {*?} defaultValue - Value to use as default one.
21
   * @return {*}
22
   */
23
  val(name, defaultValue = undefined) {
24
25
    const envVarName = this.prefix + name;
26
    const value = process.env[envVarName] || defaultValue;
27
28
    if (value === undefined) {
29
      throw new Error(`Required ENV variable is not defined: "${envVarName}".`);
30
    }
31
32
    if (String(value).toLowerCase() === 'null') {
33
      return null;
34
    }
35
36
    return value;
37
  }
38
39
  /**
40
   * Creates a new configuration entry and returns it's value in string format.
41
   *
42
   * @param {String} name - Name of the ENV variable.
43
   * @param {String|undefined?} defaultValue - Value to use as default one.
44
   * @return {String}
45
   */
46
  string(name, defaultValue = undefined) {
47
    return this.val(name, defaultValue);
48
  }
49
50
  /**
51
   * Creates a new configuration entry and returns it's value in number format.
52
   *
53
   * @param {String} name - Name of the ENV variable.
54
   * @param {Number|undefined?} defaultValue - Value to use as default one.
55
   * @return {Number}
56
   */
57
  number(name, defaultValue = undefined) {
58
    const value = this.val(name, defaultValue);
59
    return (value === null) ? null : Number(value);
60
  }
61
62
  /**
63
   * Creates a new configuration entry and returns it's value in boolean format.
64
   *
65
   * @param {String} name - Name of the ENV variable.
66
   * @param {Boolean|undefined?} defaultValue - Value to use as default one.
67
   * @return {Boolean}
68
   */
69
  boolean(name, defaultValue = undefined) {
70
    const value = this.val(name, defaultValue);
71
    return (value === null) ? null : ConfigParser.parseBooleanValue(value);
72
  }
73
74
  /**
75
   * Creates a new configuration entry and checks that it's value is in predefined range.
76
   *
77
   * @param {String} name - Name of the ENV variable.
78
   * @param {*?} defaultValue - Value to use as default one.
79
   * @param {Array?} range - A list of accepted values.
80
   * @return {*}
81
   */
82
  range(name, defaultValue = undefined, range = []) {
83
84
    const envVarName = this.prefix + name;
85
    const value = this.val(name, defaultValue);
86
87
    if (value === null) {
88
      return null;
89
    }
90
91
    if (range.length && range.indexOf(value) < 0) {
92
      throw new Error(`Given value for ENV variable "${envVarName}" is not accepted.`);
93
    }
94
95
    return value;
96
  }
97
98
  /**
99
   * Parse string and extract boolean from it.
100
   *
101
   * @param {String} value - Value to parse.
102
   * @return {Boolean} Operation result.
103
   */
104
  static parseBooleanValue(value) {
105
    const trueValues = ['true', 't', 'yes', 'y', 'on', '1'];
106
    return (trueValues.indexOf(String(value).toLowerCase()) >= 0);
107
  }
108
}
109
110
module.exports = ConfigParser;
111