merge-strategy.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @filedescription Merge Strategy
  3. */
  4. "use strict";
  5. //-----------------------------------------------------------------------------
  6. // Class
  7. //-----------------------------------------------------------------------------
  8. /**
  9. * Container class for several different merge strategies.
  10. */
  11. class MergeStrategy {
  12. /**
  13. * Merges two keys by overwriting the first with the second.
  14. * @param {*} value1 The value from the first object key.
  15. * @param {*} value2 The value from the second object key.
  16. * @returns {*} The second value.
  17. */
  18. static overwrite(value1, value2) {
  19. return value2;
  20. }
  21. /**
  22. * Merges two keys by replacing the first with the second only if the
  23. * second is defined.
  24. * @param {*} value1 The value from the first object key.
  25. * @param {*} value2 The value from the second object key.
  26. * @returns {*} The second value if it is defined.
  27. */
  28. static replace(value1, value2) {
  29. if (typeof value2 !== "undefined") {
  30. return value2;
  31. }
  32. return value1;
  33. }
  34. /**
  35. * Merges two properties by assigning properties from the second to the first.
  36. * @param {*} value1 The value from the first object key.
  37. * @param {*} value2 The value from the second object key.
  38. * @returns {*} A new object containing properties from both value1 and
  39. * value2.
  40. */
  41. static assign(value1, value2) {
  42. return Object.assign({}, value1, value2);
  43. }
  44. }
  45. exports.MergeStrategy = MergeStrategy;