You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.4 KiB

4 years ago
  1. 'use strict';
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3. module.exports = function mergeRecursive(items) {
  4. var merge = function merge(target, source) {
  5. var merged = {};
  6. var mergedKeys = Object.keys(Object.assign({}, target, source));
  7. mergedKeys.forEach(function (key) {
  8. if (target[key] === undefined && source[key] !== undefined) {
  9. merged[key] = source[key];
  10. } else if (target[key] !== undefined && source[key] === undefined) {
  11. merged[key] = target[key];
  12. } else if (target[key] !== undefined && source[key] !== undefined) {
  13. if (target[key] === source[key]) {
  14. merged[key] = target[key];
  15. } else if (!Array.isArray(target[key]) && _typeof(target[key]) === 'object' && !Array.isArray(source[key]) && _typeof(source[key]) === 'object') {
  16. merged[key] = merge(target[key], source[key]);
  17. } else {
  18. merged[key] = [].concat(target[key], source[key]);
  19. }
  20. }
  21. });
  22. return merged;
  23. };
  24. if (!items) {
  25. return this;
  26. }
  27. if (items.constructor.name === 'Collection') {
  28. return new this.constructor(merge(this.items, items.all()));
  29. }
  30. return new this.constructor(merge(this.items, items));
  31. };