All files / server/src/config env.js

95.65% Statements 22/23
91.67% Branches 11/12
100% Functions 4/4
100% Lines 18/18
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  27x     27x 6264x 5427x 4860x     27x 7857x 6264x 6264x 5427x 4860x 4860x   27x   4698x     27x 27x 7857x     27x  
// 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);
  try {
    if (is.json(str)) return JSON.parse(str);
  } catch (err) {
    return str;
  }
  return str;
};
 
const env = {};
for (let key in process.env) {
  env[key] = type(process.env[key]);
}
 
module.exports = env;