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.

97 lines
2.8 KiB

4 years ago
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global.deepmerge = factory());
  5. }(this, (function () { 'use strict';
  6. var isMergeableObject = function isMergeableObject(value) {
  7. return isNonNullObject(value)
  8. && !isSpecial(value)
  9. };
  10. function isNonNullObject(value) {
  11. return !!value && typeof value === 'object'
  12. }
  13. function isSpecial(value) {
  14. var stringValue = Object.prototype.toString.call(value);
  15. return stringValue === '[object RegExp]'
  16. || stringValue === '[object Date]'
  17. || isReactElement(value)
  18. }
  19. // see https://github.com/facebook/react/blob/b5ac963fb791d1298e7f396236383bc955f916c1/src/isomorphic/classic/element/ReactElement.js#L21-L25
  20. var canUseSymbol = typeof Symbol === 'function' && Symbol.for;
  21. var REACT_ELEMENT_TYPE = canUseSymbol ? Symbol.for('react.element') : 0xeac7;
  22. function isReactElement(value) {
  23. return value.$$typeof === REACT_ELEMENT_TYPE
  24. }
  25. function emptyTarget(val) {
  26. return Array.isArray(val) ? [] : {}
  27. }
  28. function cloneUnlessOtherwiseSpecified(value, options) {
  29. return (options.clone !== false && options.isMergeableObject(value))
  30. ? deepmerge(emptyTarget(value), value, options)
  31. : value
  32. }
  33. function defaultArrayMerge(target, source, options) {
  34. return target.concat(source).map(function(element) {
  35. return cloneUnlessOtherwiseSpecified(element, options)
  36. })
  37. }
  38. function mergeObject(target, source, options) {
  39. var destination = {};
  40. if (options.isMergeableObject(target)) {
  41. Object.keys(target).forEach(function(key) {
  42. destination[key] = cloneUnlessOtherwiseSpecified(target[key], options);
  43. });
  44. }
  45. Object.keys(source).forEach(function(key) {
  46. if (!options.isMergeableObject(source[key]) || !target[key]) {
  47. destination[key] = cloneUnlessOtherwiseSpecified(source[key], options);
  48. } else {
  49. destination[key] = deepmerge(target[key], source[key], options);
  50. }
  51. });
  52. return destination
  53. }
  54. function deepmerge(target, source, options) {
  55. options = options || {};
  56. options.arrayMerge = options.arrayMerge || defaultArrayMerge;
  57. options.isMergeableObject = options.isMergeableObject || isMergeableObject;
  58. var sourceIsArray = Array.isArray(source);
  59. var targetIsArray = Array.isArray(target);
  60. var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray;
  61. if (!sourceAndTargetTypesMatch) {
  62. return cloneUnlessOtherwiseSpecified(source, options)
  63. } else if (sourceIsArray) {
  64. return options.arrayMerge(target, source, options)
  65. } else {
  66. return mergeObject(target, source, options)
  67. }
  68. }
  69. deepmerge.all = function deepmergeAll(array, options) {
  70. if (!Array.isArray(array)) {
  71. throw new Error('first argument should be an array')
  72. }
  73. return array.reduce(function(prev, next) {
  74. return deepmerge(prev, next, options)
  75. }, {})
  76. };
  77. var deepmerge_1 = deepmerge;
  78. return deepmerge_1;
  79. })));