all files / util/ cloneDeep.js

93.33% Statements 14/15
87.5% Branches 7/8
100% Functions 3/3
93.33% Lines 14/15
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          55631× 6803×         48828×     48828× 11876×       36952×     11876× 11876× 34903×   11876×     6803×      
import isObject from './isObject'
import isArray from './isArray'
import forEach from './forEach'
import platform from './platform'
 
function cloneDeep(val) {
  if (isArray(val)) {
    return _cloneArrayDeep(val);
  }
  // HACK: should we clone Files?
  // ATM we only use it when creating FileNodes
  // where the File instance is actually not serialized
  Iif (platform.inBrowser && val instanceof window.File) {
    return val
  }
  if (isObject(val)) {
    return _cloneObjectDeep(val)
  }
  // primitives don't need to be cloned
  // TODO: is that ok?
  return val
}
 
function _cloneObjectDeep(obj) {
  let res = {}
  forEach(obj, (val, key) => {
    res[key] = cloneDeep(val)
  })
  return res
}
 
function _cloneArrayDeep(arr) {
  return arr.map(cloneDeep)
}
 
export default cloneDeep