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 | // see https://github.com/facebook/react/blob/b5ac963fb791d1298e7f396236383bc955f916c1/src/isomorphic/classic/element/ReactElement.js#L21-L25
|
---|
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 Object.propertyIsEnumerable.call(target, symbol)
|
---|
56 | })
|
---|
57 | : []
|
---|
58 | }
|
---|
59 |
|
---|
60 | function getKeys(target) {
|
---|
61 | return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target))
|
---|
62 | }
|
---|
63 |
|
---|
64 | function propertyIsOnObject(object, property) {
|
---|
65 | try {
|
---|
66 | return property in object
|
---|
67 | } catch(_) {
|
---|
68 | return false
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | // Protects from prototype poisoning and unexpected merging up the prototype chain.
|
---|
73 | function propertyIsUnsafe(target, key) {
|
---|
74 | return propertyIsOnObject(target, key) // Properties are safe to merge if they don't exist in the target yet,
|
---|
75 | && !(Object.hasOwnProperty.call(target, key) // unsafe if they exist up the prototype chain,
|
---|
76 | && Object.propertyIsEnumerable.call(target, key)) // and also unsafe if they're nonenumerable.
|
---|
77 | }
|
---|
78 |
|
---|
79 | function mergeObject(target, source, options) {
|
---|
80 | var destination = {};
|
---|
81 | if (options.isMergeableObject(target)) {
|
---|
82 | getKeys(target).forEach(function(key) {
|
---|
83 | destination[key] = cloneUnlessOtherwiseSpecified(target[key], options);
|
---|
84 | });
|
---|
85 | }
|
---|
86 | getKeys(source).forEach(function(key) {
|
---|
87 | if (propertyIsUnsafe(target, key)) {
|
---|
88 | return
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (propertyIsOnObject(target, key) && options.isMergeableObject(source[key])) {
|
---|
92 | destination[key] = getMergeFunction(key, options)(target[key], source[key], options);
|
---|
93 | } else {
|
---|
94 | destination[key] = cloneUnlessOtherwiseSpecified(source[key], options);
|
---|
95 | }
|
---|
96 | });
|
---|
97 | return destination
|
---|
98 | }
|
---|
99 |
|
---|
100 | function deepmerge(target, source, options) {
|
---|
101 | options = options || {};
|
---|
102 | options.arrayMerge = options.arrayMerge || defaultArrayMerge;
|
---|
103 | options.isMergeableObject = options.isMergeableObject || isMergeableObject;
|
---|
104 | // cloneUnlessOtherwiseSpecified is added to `options` so that custom arrayMerge()
|
---|
105 | // implementations can use it. The caller may not replace it.
|
---|
106 | options.cloneUnlessOtherwiseSpecified = cloneUnlessOtherwiseSpecified;
|
---|
107 |
|
---|
108 | var sourceIsArray = Array.isArray(source);
|
---|
109 | var targetIsArray = Array.isArray(target);
|
---|
110 | var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray;
|
---|
111 |
|
---|
112 | if (!sourceAndTargetTypesMatch) {
|
---|
113 | return cloneUnlessOtherwiseSpecified(source, options)
|
---|
114 | } else if (sourceIsArray) {
|
---|
115 | return options.arrayMerge(target, source, options)
|
---|
116 | } else {
|
---|
117 | return mergeObject(target, source, options)
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | deepmerge.all = function deepmergeAll(array, options) {
|
---|
122 | if (!Array.isArray(array)) {
|
---|
123 | throw new Error('first argument should be an array')
|
---|
124 | }
|
---|
125 |
|
---|
126 | return array.reduce(function(prev, next) {
|
---|
127 | return deepmerge(prev, next, options)
|
---|
128 | }, {})
|
---|
129 | };
|
---|
130 |
|
---|
131 | var deepmerge_1 = deepmerge;
|
---|
132 |
|
---|
133 | module.exports = deepmerge_1;
|
---|