cjs.js 3.2 KB

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