You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1023 B
50 lines
1023 B
4 years ago
|
'use strict';
|
||
|
|
||
|
/**
|
||
|
* Module dependenices
|
||
|
*/
|
||
|
|
||
|
const clone = require('shallow-clone');
|
||
|
const typeOf = require('kind-of');
|
||
|
const isPlainObject = require('is-plain-object');
|
||
|
|
||
|
function cloneDeep(val, instanceClone) {
|
||
|
switch (typeOf(val)) {
|
||
|
case 'object':
|
||
|
return cloneObjectDeep(val, instanceClone);
|
||
|
case 'array':
|
||
|
return cloneArrayDeep(val, instanceClone);
|
||
|
default: {
|
||
|
return clone(val);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function cloneObjectDeep(val, instanceClone) {
|
||
|
if (typeof instanceClone === 'function') {
|
||
|
return instanceClone(val);
|
||
|
}
|
||
|
if (instanceClone || isPlainObject(val)) {
|
||
|
const res = new val.constructor();
|
||
|
for (let key in val) {
|
||
|
res[key] = cloneDeep(val[key], instanceClone);
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
return val;
|
||
|
}
|
||
|
|
||
|
function cloneArrayDeep(val, instanceClone) {
|
||
|
const res = new val.constructor(val.length);
|
||
|
for (let i = 0; i < val.length; i++) {
|
||
|
res[i] = cloneDeep(val[i], instanceClone);
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Expose `cloneDeep`
|
||
|
*/
|
||
|
|
||
|
module.exports = cloneDeep;
|