All files utils.js

56.67% Statements 17/30
44.83% Branches 13/29
25% Functions 1/4
56.67% Lines 17/30
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                                          4x 1x   3x 3x 3x 3x   3x 1x         1x 1x   1x 1x     1x 1x 1x 1x                       3x    
export {deepFreeze, clone}
 
function deepFreeze(o) {
  Object.freeze(o)
 
  Object.getOwnPropertyNames(o).forEach(prop => {
    if (
      o.hasOwnProperty(prop) &&
      o[prop] !== null &&
      (typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
      !Object.isFrozen(o[prop])
    ) {
      deepFreeze(o[prop])
    }
  })
 
  return o
}
 
function clone(item) {
  /* eslint complexity:[2, 11] max-depth:[2, 6] */
  if (!item) {
    return item
  }
  const type = typeof item
  const string = Object.prototype.toString.call(item)
  const isPrimitive = type !== 'object' && type !== 'function'
  let result = item
 
  if (!isPrimitive) {
    Iif (string === '[object Array]') {
      result = []
      item.forEach((child, index) => {
        result[index] = clone(child)
      })
    } else Eif (type === 'object') {
      Iif (item.nodeType && typeof item.cloneNode === 'function') {
        result = item.cloneNode(true)
      } else Eif (!item.prototype) {
        Iif (string === '[object Date]') {
          result = new Date(item)
        } else {
          result = {}
          for (const i in item) {
            Eif (item.hasOwnProperty(i)) {
              result[i] = clone(item[i])
            }
          }
        }
      } else if (item.constructor) {
        result = new item.constructor()
      } else {
        result = item
      }
    }
  }
 
  return result
}