From aa059ff617f0902a1c15443aa517f07bdbc64bea Mon Sep 17 00:00:00 2001 From: hcong <1050828145@qq.com> Date: 星期五, 27 九月 2024 15:50:03 +0800 Subject: [PATCH] Merge branch 'hc-evalution' of ssh://114.215.109.124:29418/supervision-vue into hc-evalution --- src/main.js | 1 src/utils/DeepCopy.js | 65 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 0 deletions(-) diff --git a/src/main.js b/src/main.js index 1f6b4b5..8a0d286 100644 --- a/src/main.js +++ b/src/main.js @@ -18,6 +18,7 @@ const app = createApp(App); app.config.globalProperties.$fm = timeUtil; +app.config.globalProperties.$deepCopy = DeepCopy.deepCopy; for (const [key, component] of Object.entries(ElementPlusIconsVue)) { app.component(key, component); diff --git a/src/utils/DeepCopy.js b/src/utils/DeepCopy.js new file mode 100644 index 0000000..5124708 --- /dev/null +++ b/src/utils/DeepCopy.js @@ -0,0 +1,65 @@ +export default { + deepCopy(target) { + const map = new WeakMap() + + function isObject(target) { + return (typeof target === 'object' && target ) || typeof target === 'function' + } + + function clone(data) { + if (!isObject(data)) { + return data + } + if ([Date, RegExp].includes(data.constructor)) { + return new data.constructor(data) + } + if (typeof data === 'function') { + return new Function('return ' + data.toString())() + } + const exist = map.get(data) + if (exist) { + return exist + } + if (data instanceof Map) { + const result = new Map() + map.set(data, result) + data.forEach((val, key) => { + if (isObject(val)) { + result.set(key, clone(val)) + } else { + result.set(key, val) + } + }) + return result + } + if (data instanceof Set) { + const result = new Set() + map.set(data, result) + data.forEach(val => { + if (isObject(val)) { + result.add(clone(val)) + } else { + result.add(val) + } + }) + return result + } + const keys = Reflect.ownKeys(data) + const allDesc = Object.getOwnPropertyDescriptors(data) + const result = Object.create(Object.getPrototypeOf(data), allDesc) + map.set(data, result) + keys.forEach(key => { + const val = data[key] + if (isObject(val)) { + result[key] = clone(val) + } else { + result[key] = val + } + }) + return result + } + // clone(target) + return JSON.parse(JSON.stringify(target)); + } + +} \ No newline at end of file -- Gitblit v1.9.3