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