UNPKG

@mr-hope/vuepress-shared

Version:
54 lines 2.36 kB
/** Deep merge objects to the first one */ export const deepAssign = (originObject, ...assignObjects) => { if (assignObjects.length === 0) return originObject; /** Object being merged */ const assignObject = assignObjects.shift(); Object.keys(assignObject).forEach((property) => { if (typeof originObject[property] === "object" && !Array.isArray(originObject[property]) && typeof assignObject[property] === "object" && !Array.isArray(assignObject[property])) deepAssign(originObject[property], assignObject[property]); else if (typeof assignObject[property] === "object") if (Array.isArray(assignObject[property])) originObject[property] = [ ...assignObject[property], ]; else originObject[property] = { ...assignObject[property], }; else originObject[property] = assignObject[property]; }); return deepAssign(originObject, ...assignObjects); }; /** Deep merge objects to the last one */ export const deepAssignReverse = (...assignObjects) => { if (assignObjects.length === 0) throw new Error("No param is given"); if (assignObjects.length === 1) return assignObjects[0]; const assignObject = assignObjects.pop(); const originObject = assignObjects.pop(); Object.keys(originObject).forEach((property) => { if (assignObject[property] === undefined) if (typeof originObject[property] === "object") if (Array.isArray(originObject[property])) assignObject[property] = [...originObject[property]]; else assignObject[property] = { ...originObject[property], }; else assignObject[property] = originObject[property]; else if (typeof assignObject[property] === "object" && !Array.isArray(assignObject) && typeof originObject[property] === "object" && !Array.isArray(originObject[property])) deepAssignReverse(originObject[property], assignObject[property]); }); return deepAssignReverse(...assignObjects, assignObject); }; //# sourceMappingURL=assign.js.map