es.js 3.2 KB

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