gitoriaLog in with ident

mpackdb

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb4db6391b4db6391initial commitcaramboleyob4db6391/src/Model.js

1.0 KB

  1. export function isObject(item) {
  2. return (item && item !== null && typeof item === 'object' && !Array.isArray(item));
  3. }
  4. // with recursion detection
  5. export function assignDeep(target, ...sources) {
  6. const seen = new WeakSet(); // Use WeakSet to store processed objects
  7. const mergeInternal = (target, source) => {
  8. if (!isObject(target) || !isObject(source)) {
  9. return;
  10. }
  11. // Check if the object has been processed before
  12. if (seen.has(source)) {
  13. return;
  14. }
  15. seen.add(source); // Mark the object as processed
  16. for (const key in source) {
  17. if (isObject(source[key]) && target[key]) {
  18. mergeInternal(target[key], source[key]);
  19. } else {
  20. try {
  21. target[key] = source[key];
  22. } catch (e) {
  23. // Handle the case where a non-writable property is tried to be set
  24. }
  25. }
  26. }
  27. };
  28. for (const source of sources) {
  29. mergeInternal(target, source);
  30. }
  31. return target;
  32. }
  33. export class Model {
  34. constructor(data = {}, merge = true) {
  35. if (merge) {
  36. assignDeep(this, data);
  37. } else {
  38. Object.assign(this, data);
  39. }
  40. }
  41. }

Branches

Latest commits

  • b4db6391initial commitcaramboleyo