All files / server/utils/options env.js

100% Statements 21/21
100% Branches 10/10
100% Functions 4/4
100% Lines 17/17
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  34x     34x 7956x 6936x 6222x     34x 9962x 7956x 6936x 6222x 6222x   34x   6018x     34x 34x 9962x     34x  
// 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;
  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;