umd.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 cloneIfNecessary(value, optionsArgument) {
  29. var clone = optionsArgument && optionsArgument.clone === true;
  30. return (clone && isMergeableObject(value)) ? deepmerge(emptyTarget(value), value, optionsArgument) : value
  31. }
  32. function defaultArrayMerge(target, source, optionsArgument) {
  33. var destination = target.slice();
  34. source.forEach(function(e, i) {
  35. if (typeof destination[i] === 'undefined') {
  36. destination[i] = cloneIfNecessary(e, optionsArgument);
  37. } else if (isMergeableObject(e)) {
  38. destination[i] = deepmerge(target[i], e, optionsArgument);
  39. } else if (target.indexOf(e) === -1) {
  40. destination.push(cloneIfNecessary(e, optionsArgument));
  41. }
  42. });
  43. return destination
  44. }
  45. function mergeObject(target, source, optionsArgument) {
  46. var destination = {};
  47. if (isMergeableObject(target)) {
  48. Object.keys(target).forEach(function(key) {
  49. destination[key] = cloneIfNecessary(target[key], optionsArgument);
  50. });
  51. }
  52. Object.keys(source).forEach(function(key) {
  53. if (!isMergeableObject(source[key]) || !target[key]) {
  54. destination[key] = cloneIfNecessary(source[key], optionsArgument);
  55. } else {
  56. destination[key] = deepmerge(target[key], source[key], optionsArgument);
  57. }
  58. });
  59. return destination
  60. }
  61. function deepmerge(target, source, optionsArgument) {
  62. var sourceIsArray = Array.isArray(source);
  63. var targetIsArray = Array.isArray(target);
  64. var options = optionsArgument || { arrayMerge: defaultArrayMerge };
  65. var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray;
  66. if (!sourceAndTargetTypesMatch) {
  67. return cloneIfNecessary(source, optionsArgument)
  68. } else if (sourceIsArray) {
  69. var arrayMerge = options.arrayMerge || defaultArrayMerge;
  70. return arrayMerge(target, source, optionsArgument)
  71. } else {
  72. return mergeObject(target, source, optionsArgument)
  73. }
  74. }
  75. deepmerge.all = function deepmergeAll(array, optionsArgument) {
  76. if (!Array.isArray(array) || array.length < 2) {
  77. throw new Error('first argument should be an array with at least two elements')
  78. }
  79. // we are sure there are at least 2 values, so it is safe to have no initial value
  80. return array.reduce(function(prev, next) {
  81. return deepmerge(prev, next, optionsArgument)
  82. })
  83. };
  84. var deepmerge_1 = deepmerge;
  85. return deepmerge_1;
  86. })));