Code coverage report for src/utility.js

Statements: 86.84% (33 / 38)      Branches: 72.22% (13 / 18)      Functions: 83.33% (5 / 6)      Lines: 79.17% (19 / 24)      Ignored: 1 statement, 1 branch     

All files » src/ » utility.js
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71          1                         1 4 2               2 1     1     2 1     1 1   1 1         1                     1 40             1 40    
import isArray from 'lodash.isarray';
import isFunction from 'lodash.isfunction';
import isString from 'lodash.isstring';
import { format } from 'util';
 
const check = {
  'array': isArray,
  'function': isFunction,
  'string': isString,
};
 
/**
 * @param  {string} prop
 * @param  {string[]} aliases
 * @param  {string} type
 * @param  {object} source
 * @return {*}
 */
export function get(prop, aliases, type, source) {
  if (source[prop]) {
    Iif (isArray(type)) {
      if (type.some(tp => is(tp, source[prop]))) {
        return source[prop];
      }
 
      throw new Error(format('should specify %s for %s', type.join('|'), prop));
    }
 
    if (!is(type, source[prop])) {
      throw new Error(format('should specify %s for %s', type, prop));
    }
 
    return source[prop];
  }
 
  if (!isArray(aliases)) {
    return null;
  }
 
  let deprecatedProp;
  const match = aliases.some(alias => Boolean(source[(deprecatedProp = alias)]));
 
  Eif (match) {
    Iif (!is(type, source[deprecatedProp])) {
      throw new Error(format('should specify %s for %s', type, deprecatedProp));
    }
 
    // deprecated message
    return source[deprecatedProp];
  }
 
  return null;
}
 
/**
 * @param  {string}  type
 * @param  {*}       value
 * @return {boolean}
 */
export function is(type, value) {
  return check[type](value);
}
 
/**
 * @param  {string} str
 * @return {string}
 */
export function removeQuotes(str) {
  return str.replace(/^["']|["']$/g, '');
}