Skip to content

Commit 1356f27

Browse files
committed
Better booleans parsing
1 parent d485be9 commit 1356f27

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

lib/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* This class is responsible for 12-factor config parsing.
33
*/
4-
class TwelveConfig {
4+
class ConfigParser {
55

66
/**
77
* Class constructor.
@@ -68,7 +68,7 @@ class TwelveConfig {
6868
*/
6969
boolean(name, defaultValue = undefined) {
7070
const value = this.val(name, defaultValue);
71-
return (value === null) ? null : Boolean(value);
71+
return (value === null) ? null : ConfigParser.parseBooleanValue(value);
7272
}
7373

7474
/**
@@ -94,6 +94,17 @@ class TwelveConfig {
9494

9595
return value;
9696
}
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+
}
97108
}
98109

99-
module.exports = TwelveConfig;
110+
module.exports = ConfigParser;

0 commit comments

Comments
 (0)