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