All files / src/config env.js

95.24% Statements 20/21
91.67% Branches 11/12
100% Functions 4/4
100% Lines 16/16
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  1x     1x 102x 90x 86x     1x 107x 102x 102x 90x 86x 82x       1x 1x 107x     1x  
// Load them from the environment file if any
require('dotenv').config({ silent: true });
 
// Check if a variable is numeric even if string
const is = {
  numeric: num => !isNaN(num),
  boolean: bool => /^(true|false)$/i.test(bool),
  json: str => /^[\{\[]/.test(str) && /[\}\]]$/.test(str)
};
 
const type = str => {
  if (!str) return;
  Iif (typeof str !== 'string') return str;
  if (is.numeric(str)) return +str;
  if (is.boolean(str)) return /true/i.test(str);
  if (is.json(str)) return JSON.parse(str);
  return str;
};
 
// Get the process variables in lowercase
const env = {};
for (let key in process.env) {
  env[key] = type(process.env[key]);
}
 
module.exports = env;