From a40e440d02e3a762d91a12d3f57649b2018b5c72 Mon Sep 17 00:00:00 2001 From: linfso Date: Fri, 15 Aug 2025 14:23:03 +0800 Subject: [PATCH] md --- common/custom.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 common/custom.js diff --git a/common/custom.js b/common/custom.js new file mode 100644 index 0000000..b7a99b0 --- /dev/null +++ b/common/custom.js @@ -0,0 +1,21 @@ +/** + * This is just a simple version of deep copy + * Has a lot of edge cases bug + * If you want to use a perfect deep copy, use lodash's _.cloneDeep + * @param {Object} source + * @returns {Object} + */ +export function deepClone(source) { + if (!source && typeof source !== 'object') { + throw new Error('error arguments', 'deepClone') + } + const targetObj = source.constructor === Array ? [] : {} + Object.keys(source).forEach(keys => { + if (source[keys] && typeof source[keys] === 'object') { + targetObj[keys] = deepClone(source[keys]) + } else { + targetObj[keys] = source[keys] + } + }) + return targetObj +} \ No newline at end of file