1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/clone-deep/index.js

50 lines
1023 B
JavaScript
Raw Permalink Normal View History

2023-12-18 13:12:25 +08:00
'use strict';
/**
* Module dependenices
*/
2024-01-16 21:26:16 +08:00
const clone = require('shallow-clone');
const typeOf = require('kind-of');
const isPlainObject = require('is-plain-object');
2023-12-18 13:12:25 +08:00
function cloneDeep(val, instanceClone) {
switch (typeOf(val)) {
case 'object':
return cloneObjectDeep(val, instanceClone);
case 'array':
return cloneArrayDeep(val, instanceClone);
default: {
return clone(val);
}
}
}
2024-01-16 21:26:16 +08:00
function cloneObjectDeep(val, instanceClone) {
2023-12-18 13:12:25 +08:00
if (typeof instanceClone === 'function') {
2024-01-16 21:26:16 +08:00
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;
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
return val;
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
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);
2023-12-18 13:12:25 +08:00
}
return res;
}
/**
* Expose `cloneDeep`
*/
module.exports = cloneDeep;