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.

50 lines
1.8 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 replaceRecursive(items) {
  4. var replace = function replace(target, source) {
  5. var replaced = Object.assign({}, target);
  6. var mergedKeys = Object.keys(Object.assign({}, target, source));
  7. mergedKeys.forEach(function (key) {
  8. if (!Array.isArray(source[key]) && _typeof(source[key]) === 'object') {
  9. replaced[key] = replace(target[key], source[key]);
  10. } else if (target[key] === undefined && source[key] !== undefined) {
  11. if (_typeof(target[key]) === 'object') {
  12. replaced[key] = Object.assign({}, source[key]);
  13. } else {
  14. replaced[key] = source[key];
  15. }
  16. } else if (target[key] !== undefined && source[key] === undefined) {
  17. if (_typeof(target[key]) === 'object') {
  18. replaced[key] = Object.assign({}, target[key]);
  19. } else {
  20. replaced[key] = target[key];
  21. }
  22. } else if (target[key] !== undefined && source[key] !== undefined) {
  23. if (_typeof(source[key]) === 'object') {
  24. replaced[key] = Object.assign({}, source[key]);
  25. } else {
  26. replaced[key] = source[key];
  27. }
  28. }
  29. });
  30. return replaced;
  31. };
  32. if (!items) {
  33. return this;
  34. }
  35. if (!Array.isArray(items) && (typeof items === 'undefined' ? 'undefined' : _typeof(items)) !== 'object') {
  36. return new this.constructor(replace(this.items, [items]));
  37. }
  38. if (items.constructor.name === 'Collection') {
  39. return new this.constructor(replace(this.items, items.all()));
  40. }
  41. return new this.constructor(replace(this.items, items));
  42. };