index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. define(factory);
  4. } else if (typeof exports === 'object') {
  5. module.exports = factory();
  6. } else {
  7. root.deepmerge = factory();
  8. }
  9. }(this, function () {
  10. function isMergeableObject(val) {
  11. var nonNullObject = val && typeof val === 'object'
  12. return nonNullObject
  13. && Object.prototype.toString.call(val) !== '[object RegExp]'
  14. && Object.prototype.toString.call(val) !== '[object Date]'
  15. }
  16. function emptyTarget(val) {
  17. return Array.isArray(val) ? [] : {}
  18. }
  19. function cloneIfNecessary(value, optionsArgument) {
  20. var clone = optionsArgument && optionsArgument.clone === true
  21. return (clone && isMergeableObject(value)) ? deepmerge(emptyTarget(value), value, optionsArgument) : value
  22. }
  23. function defaultArrayMerge(target, source, optionsArgument) {
  24. var destination = target.slice()
  25. source.forEach(function(e, i) {
  26. if (typeof destination[i] === 'undefined') {
  27. destination[i] = cloneIfNecessary(e, optionsArgument)
  28. } else if (isMergeableObject(e)) {
  29. destination[i] = deepmerge(target[i], e, optionsArgument)
  30. } else if (target.indexOf(e) === -1) {
  31. destination.push(cloneIfNecessary(e, optionsArgument))
  32. }
  33. })
  34. return destination
  35. }
  36. function mergeObject(target, source, optionsArgument) {
  37. var destination = {}
  38. if (isMergeableObject(target)) {
  39. Object.keys(target).forEach(function (key) {
  40. destination[key] = cloneIfNecessary(target[key], optionsArgument)
  41. })
  42. }
  43. Object.keys(source).forEach(function (key) {
  44. if (!isMergeableObject(source[key]) || !target[key]) {
  45. destination[key] = cloneIfNecessary(source[key], optionsArgument)
  46. } else {
  47. destination[key] = deepmerge(target[key], source[key], optionsArgument)
  48. }
  49. })
  50. return destination
  51. }
  52. function deepmerge(target, source, optionsArgument) {
  53. var array = Array.isArray(source);
  54. var options = optionsArgument || { arrayMerge: defaultArrayMerge }
  55. var arrayMerge = options.arrayMerge || defaultArrayMerge
  56. if (array) {
  57. return Array.isArray(target) ? arrayMerge(target, source, optionsArgument) : cloneIfNecessary(source, optionsArgument)
  58. } else {
  59. return mergeObject(target, source, optionsArgument)
  60. }
  61. }
  62. deepmerge.all = function deepmergeAll(array, optionsArgument) {
  63. if (!Array.isArray(array) || array.length < 2) {
  64. throw new Error('first argument should be an array with at least two elements')
  65. }
  66. // we are sure there are at least 2 values, so it is safe to have no initial value
  67. return array.reduce(function(prev, next) {
  68. return deepmerge(prev, next, optionsArgument)
  69. })
  70. }
  71. return deepmerge
  72. }));