1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
3 | typeof define === 'function' && define.amd ? define(factory) :
|
4 | (global = global || self, global.deepmerge = factory());
|
5 | }(this, function () { 'use strict';
|
6 |
|
7 | var isMergeableObject = function isMergeableObject(value) {
|
8 | return isNonNullObject(value)
|
9 | && !isSpecial(value)
|
10 | };
|
11 |
|
12 | function isNonNullObject(value) {
|
13 | return !!value && typeof value === 'object'
|
14 | }
|
15 |
|
16 | function isSpecial(value) {
|
17 | var stringValue = Object.prototype.toString.call(value);
|
18 |
|
19 | return stringValue === '[object RegExp]'
|
20 | || stringValue === '[object Date]'
|
21 | || isReactElement(value)
|
22 | }
|
23 |
|
24 |
|
25 | var canUseSymbol = typeof Symbol === 'function' && Symbol.for;
|
26 | var REACT_ELEMENT_TYPE = canUseSymbol ? Symbol.for('react.element') : 0xeac7;
|
27 |
|
28 | function isReactElement(value) {
|
29 | return value.$$typeof === REACT_ELEMENT_TYPE
|
30 | }
|
31 |
|
32 | function emptyTarget(val) {
|
33 | return Array.isArray(val) ? [] : {}
|
34 | }
|
35 |
|
36 | function cloneUnlessOtherwiseSpecified(value, options) {
|
37 | return (options.clone !== false && options.isMergeableObject(value))
|
38 | ? deepmerge(emptyTarget(value), value, options)
|
39 | : value
|
40 | }
|
41 |
|
42 | function defaultArrayMerge(target, source, options) {
|
43 | return target.concat(source).map(function(element) {
|
44 | return cloneUnlessOtherwiseSpecified(element, options)
|
45 | })
|
46 | }
|
47 |
|
48 | function getMergeFunction(key, options) {
|
49 | if (!options.customMerge) {
|
50 | return deepmerge
|
51 | }
|
52 | var customMerge = options.customMerge(key);
|
53 | return typeof customMerge === 'function' ? customMerge : deepmerge
|
54 | }
|
55 |
|
56 | function getEnumerableOwnPropertySymbols(target) {
|
57 | return Object.getOwnPropertySymbols
|
58 | ? Object.getOwnPropertySymbols(target).filter(function(symbol) {
|
59 | return target.propertyIsEnumerable(symbol)
|
60 | })
|
61 | : []
|
62 | }
|
63 |
|
64 | function getKeys(target) {
|
65 | return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target))
|
66 | }
|
67 |
|
68 | function mergeObject(target, source, options) {
|
69 | var destination = {};
|
70 | if (options.isMergeableObject(target)) {
|
71 | getKeys(target).forEach(function(key) {
|
72 | destination[key] = cloneUnlessOtherwiseSpecified(target[key], options);
|
73 | });
|
74 | }
|
75 | getKeys(source).forEach(function(key) {
|
76 | if (!options.isMergeableObject(source[key]) || !target[key]) {
|
77 | destination[key] = cloneUnlessOtherwiseSpecified(source[key], options);
|
78 | } else {
|
79 | destination[key] = getMergeFunction(key, options)(target[key], source[key], options);
|
80 | }
|
81 | });
|
82 | return destination
|
83 | }
|
84 |
|
85 | function deepmerge(target, source, options) {
|
86 | options = Object.assign({
|
87 | arrayMerge: defaultArrayMerge,
|
88 | isMergeableObject: isMergeableObject
|
89 | }, options, {
|
90 | cloneUnlessOtherwiseSpecified: cloneUnlessOtherwiseSpecified
|
91 | });
|
92 |
|
93 | var sourceIsArray = Array.isArray(source);
|
94 | var targetIsArray = Array.isArray(target);
|
95 | var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray;
|
96 |
|
97 | if (!sourceAndTargetTypesMatch) {
|
98 | return cloneUnlessOtherwiseSpecified(source, options)
|
99 | } else if (sourceIsArray) {
|
100 | return options.arrayMerge(target, source, options)
|
101 | } else {
|
102 | return mergeObject(target, source, options)
|
103 | }
|
104 | }
|
105 |
|
106 | deepmerge.all = function deepmergeAll(array, options) {
|
107 | if (!Array.isArray(array)) {
|
108 | throw new Error('first argument should be an array')
|
109 | }
|
110 |
|
111 | return array.reduce(function(prev, next) {
|
112 | return deepmerge(prev, next, options)
|
113 | }, {})
|
114 | };
|
115 |
|
116 | var deepmerge_1 = deepmerge;
|
117 |
|
118 | return deepmerge_1;
|
119 |
|
120 | }));
|