mpackdb
All repositories: gitoria
1.0 KB
export function isObject(item) {return (item && item !== null && typeof item === 'object' && !Array.isArray(item));}// with recursion detectionexport function assignDeep(target, ...sources) {const seen = new WeakSet(); // Use WeakSet to store processed objectsconst mergeInternal = (target, source) => {if (!isObject(target) || !isObject(source)) {return;}// Check if the object has been processed beforeif (seen.has(source)) {return;}seen.add(source); // Mark the object as processedfor (const key in source) {if (isObject(source[key]) && target[key]) {mergeInternal(target[key], source[key]);} else {try {target[key] = source[key];} catch (e) {// Handle the case where a non-writable property is tried to be set}}}};for (const source of sources) {mergeInternal(target, source);}return target;}export class Model {constructor(data = {}, merge = true) {if (merge) {assignDeep(this, data);} else {Object.assign(this, data);}}}
Branches
- mastermain branch