MCPcopy Create free account
hub / github.com/Useful-Scripts-Extension/useful-script / mergeObj

Function mergeObj

scripts/libs/utils/object.js:59–89  ·  view source on GitHub ↗
(objA, objB, concatArr)

Source from the content-addressed store, hash-verified

57 * @returns {*|void}
58 */
59export function mergeObj(objA, objB, concatArr) {
60 function isObj(obj) {
61 return Object.prototype.toString.call(obj) === "[object Object]";
62 }
63 function isArr(arr) {
64 return Object.prototype.toString.call(arr) === "[object Array]";
65 }
66 if (!isObj(objA) || !isObj(objB)) return objA;
67 function deepMerge(objA, objB) {
68 forIn(objB, function (key) {
69 const subItemA = objA[key];
70 const subItemB = objB[key];
71 if (typeof subItemA === "undefined") {
72 objA[key] = subItemB;
73 } else {
74 if (isObj(subItemA) && isObj(subItemB)) {
75 /* 进行深层合并 */
76 objA[key] = deepMerge(subItemA, subItemB);
77 } else {
78 if (concatArr && isArr(subItemA) && isArr(subItemB)) {
79 objA[key] = subItemA.concat(subItemB);
80 } else {
81 objA[key] = subItemB;
82 }
83 }
84 }
85 });
86 return objA;
87 }
88 return deepMerge(objA, objB);
89}
90
91/**
92 * Deep merge of multiple objects, the merge rules are based on mergeObj, but the concatArr option does not exist

Callers 1

mergeFunction · 0.85

Calls 2

deepMergeFunction · 0.85
isObjFunction · 0.70

Tested by

no test coverage detected