All files / src/config type.js

92.86% Statements 13/14
81.82% Branches 9/11
100% Functions 4/4
92.86% Lines 13/14
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    1x 105x 88x 88x     1x 105x       105x 17x     88x 4x     88x 4x     88x    
 
// 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)
};
 
module.exports = (str = '') => {
  Iif (typeof str !== 'string') {
    return str;
  }
 
  if (is.numeric(str)) {
    return +str;
  }
 
  if (is.boolean(str)) {
    str = /true/i.test(str);
  }
 
  if (is.json(str)) {
    str = JSON.parse(str);
  }
 
  return str;
};