7 const clone = require('shallow-clone');
8 const typeOf = require('kind-of');
9 const isPlainObject = require('is-plain-object');
11 function cloneDeep(val, instanceClone) {
12 switch (typeOf(val)) {
14 return cloneObjectDeep(val, instanceClone);
16 return cloneArrayDeep(val, instanceClone);
23 function cloneObjectDeep(val, instanceClone) {
24 if (typeof instanceClone === 'function') {
25 return instanceClone(val);
27 if (instanceClone || isPlainObject(val)) {
28 const res = new val.constructor();
29 for (let key in val) {
30 res[key] = cloneDeep(val[key], instanceClone);
37 function cloneArrayDeep(val, instanceClone) {
38 const res = new val.constructor(val.length);
39 for (let i = 0; i < val.length; i++) {
40 res[i] = cloneDeep(val[i], instanceClone);
49 module.exports = cloneDeep;