|
@@ -70,3 +70,34 @@ export function objectToUint8Array(data: object | string) {
|
|
|
// return outputArray;
|
|
// return outputArray;
|
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 对象深度合并
|
|
|
|
|
+ * @param target 目标对象
|
|
|
|
|
+ * @param source 源对象
|
|
|
|
|
+ * @returns T
|
|
|
|
|
+ */
|
|
|
|
|
+export const deepMerge = <T>(target: T, source: T): T => {
|
|
|
|
|
+ for (const key in source) {
|
|
|
|
|
+ const prototype = Object.prototype.toString.call(target[key]),
|
|
|
|
|
+ t = target[key],
|
|
|
|
|
+ s = source[key];
|
|
|
|
|
+ // 对象属性类型检查
|
|
|
|
|
+ switch (prototype) {
|
|
|
|
|
+ case '[object Object]':
|
|
|
|
|
+ target[key] = deepMerge(t, s);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case '[object Array]':
|
|
|
|
|
+ if (Array.isArray(t) && Array.isArray(s)) {
|
|
|
|
|
+ target[key] = t.reduce((prev, cur, i) => {
|
|
|
|
|
+ const value = deepMerge(cur, s[i]);
|
|
|
|
|
+ prev.push(value);
|
|
|
|
|
+ return prev;
|
|
|
|
|
+ }, [])
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ default:
|
|
|
|
|
+ target[key] = s;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return target;
|
|
|
|
|
+}
|