All files / shared utils.js

53.57% Statements 15/28
56.25% Branches 18/32
62.5% Functions 5/8
55.56% Lines 15/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104              191x 2x     189x                                                                                       15x               53x 1x     52x 1x     51x                   17x 2x     15x     13x     2x                   22x    
/**
 * Convenience wrapper to shorten the hasOwnProperty call
 *
 * @param {Object} object
 * @param {String} property
 */
export function hasOwn (object, property) {
  if (object === null || typeof object === 'undefined') {
    return false
  }
 
  return Object.prototype.hasOwnProperty.call(object, property)
}
 
/**
 * Performs a deep merge of `source` into `target`.
 * Mutates `target` only but not its objects and arrays.
 *
 * @author inspired by [jhildenbiddle](https://stackoverflow.com/a/48218209).
 *
 * @param {Object} target
 * @param {Object} source
 *
 * @return {Object} merged Orbject
 */
export function deepMerge (target, source) {
  const isObject = (obj) => obj && typeof obj === 'object'
 
  if (!isObject(target) || !isObject(source)) {
    return source
  }
 
  Object.keys(source).forEach(key => {
    const targetValue = target[key]
    const sourceValue = source[key]
 
    if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
      target[key] = targetValue.concat(sourceValue)
    } else if (isObject(targetValue) && isObject(sourceValue)) {
      target[key] = deepMerge(Object.assign({}, targetValue), sourceValue)
    } else {
      target[key] = sourceValue
    }
  })
 
  return target
}
 
/**
 * De-reference objects
 *
 * @param {object} obj
 * @return {object} a dereferenced copy of the passed object
 */
export function deref (obj) {
  return JSON.parse(JSON.stringify(obj))
}
 
/**
 *
 * @param {String} uri
 */
export function isAbsoluteUri (uri) {
  if (uri.indexOf('http://') === 0) {
    return true
  }
 
  if (uri.indexOf('https://') === 0) {
    return true
  }
 
  return !!uri.match(/\/\/.+/)
}
 
/**
 *
 * @param {object} config
 * @param {String} property
 * @param {Boolean} isRequiredProp
 */
export function checkConfigProperty (config, property, isRequiredProp = true) {
  if (config !== null && hasOwn(config, property)) {
    return true
  }
 
  if ((config === null ||
    hasOwn(config, property) === false) &&
    !isRequiredProp) {
    return false
  }
 
  throw new Error('Missing configuration property: ' + property)
}
 
/**
 * Make sure a passed parameter is actually a function
 *
 * @param fn
 * @returns {boolean}
 */
export function validateCallbackFn (fn) {
  return fn instanceof Function
}