All files index.js

100% Statements 2/2
100% Branches 6/6
100% Functions 1/1
100% Lines 2/2
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57  1x   1x                                                                                                          
 
import Debug from 'debug';
 
const debug = new Debug('dotenv-parse-variables');
 
export default (env) => {
 
  Object.keys(env).forEach(key => {
    debug(`key "${key}" before type was ${typeof env[key]}`);
    process.env[key] = env[key] = parseKey(env[key], key);
    debug(`key "${key}" after type was ${typeof env[key]}`);
  });
 
  return env;
 
};
 
function parseKey(value, key) {
 
  debug(`parsing key ${key} with value ${value}`);
 
  // if the value is wrapped in bacticks e.g. (`value`) then just return its value
  if (value.toString().indexOf('`') === 0
    && value.toString().lastIndexOf('`') === value.toString().length - 1) {
    debug(`key ${key} is wrapped in bacticks and will be ignored from parsing`);
    return value.toString().substring(1, value.toString().length - 1);
  }
 
  // if the value ends in an asterisk then just return its value
  if (value.toString().lastIndexOf('*') === value.toString().length - 1
    && value.toString().indexOf(',') === -1) {
    debug(`key ${key} ended in * and will be ignored from parsing`);
    return value.toString().substring(0, value.toString().length - 1);
  }
 
  // Boolean
  if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
    debug(`key ${key} parsed as a Boolean`);
    return value === 'true';
  }
 
  // Number
  if (!isNaN(value)) {
    debug(`key ${key} parsed as a Number`);
    return Number(value);
  }
 
  // Array
  if (value.indexOf(',') !== -1) {
    debug(`key ${key} parsed as an Array`);
    return value.split(',').map(parseKey);
  }
 
  return value;
 
}