merge.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. exports.__esModule = true;
  14. exports.merge = void 0;
  15. var util_js_1 = require("./util.js");
  16. var withDefaultOptions = function (options) {
  17. return __assign({ arrayMergeType: 'combine', arrayMerge: (0, util_js_1.getMergeFn)(options ? options.arrayMergeType : 'combine') }, options);
  18. };
  19. var merge = function (objects, options) {
  20. var opts = withDefaultOptions(options);
  21. return objects.reduce(function (prev, curr) {
  22. Object.keys(curr).forEach(function (key) {
  23. if (Array.isArray(prev[key]) && Array.isArray(curr[key])) {
  24. if (opts && opts.arrayMerge) {
  25. prev[key] = opts.arrayMerge(prev[key], curr[key]);
  26. }
  27. }
  28. else if ((0, util_js_1.isObject)(prev[key]) && (0, util_js_1.isObject)(curr[key])) {
  29. prev[key] = (0, exports.merge)([prev[key], curr[key]], opts);
  30. }
  31. else {
  32. prev[key] = curr[key];
  33. }
  34. });
  35. return prev;
  36. }, {});
  37. };
  38. exports.merge = merge;