combine-d9581036.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. define(['exports', './defaultValue-fe22d8c0'], (function (exports, defaultValue) { 'use strict';
  2. /**
  3. * Merges two objects, copying their properties onto a new combined object. When two objects have the same
  4. * property, the value of the property on the first object is used. If either object is undefined,
  5. * it will be treated as an empty object.
  6. *
  7. * @example
  8. * const object1 = {
  9. * propOne : 1,
  10. * propTwo : {
  11. * value1 : 10
  12. * }
  13. * }
  14. * const object2 = {
  15. * propTwo : 2
  16. * }
  17. * const final = Cesium.combine(object1, object2);
  18. *
  19. * // final === {
  20. * // propOne : 1,
  21. * // propTwo : {
  22. * // value1 : 10
  23. * // }
  24. * // }
  25. *
  26. * @param {object} [object1] The first object to merge.
  27. * @param {object} [object2] The second object to merge.
  28. * @param {boolean} [deep=false] Perform a recursive merge.
  29. * @returns {object} The combined object containing all properties from both objects.
  30. *
  31. * @function
  32. */
  33. function combine(object1, object2, deep) {
  34. deep = defaultValue.defaultValue(deep, false);
  35. const result = {};
  36. const object1Defined = defaultValue.defined(object1);
  37. const object2Defined = defaultValue.defined(object2);
  38. let property;
  39. let object1Value;
  40. let object2Value;
  41. if (object1Defined) {
  42. for (property in object1) {
  43. if (object1.hasOwnProperty(property)) {
  44. object1Value = object1[property];
  45. if (
  46. object2Defined &&
  47. deep &&
  48. typeof object1Value === "object" &&
  49. object2.hasOwnProperty(property)
  50. ) {
  51. object2Value = object2[property];
  52. if (typeof object2Value === "object") {
  53. result[property] = combine(object1Value, object2Value, deep);
  54. } else {
  55. result[property] = object1Value;
  56. }
  57. } else {
  58. result[property] = object1Value;
  59. }
  60. }
  61. }
  62. }
  63. if (object2Defined) {
  64. for (property in object2) {
  65. if (
  66. object2.hasOwnProperty(property) &&
  67. !result.hasOwnProperty(property)
  68. ) {
  69. object2Value = object2[property];
  70. result[property] = object2Value;
  71. }
  72. }
  73. }
  74. return result;
  75. }
  76. exports.combine = combine;
  77. }));