|
1 |
| -import { isChange, getChangeValue, normalizeObject } from 'validated-changeset'; |
| 1 | +import { |
| 2 | + isChange, |
| 3 | + getChangeValue, |
| 4 | + normalizeObject, |
| 5 | + isArrayObject, |
| 6 | + objectToArray, |
| 7 | + arrayToObject, |
| 8 | +} from 'validated-changeset'; |
2 | 9 |
|
3 | 10 | function isMergeableObject(value) {
|
4 | 11 | return isNonNullObject(value) && !isSpecial(value);
|
@@ -146,17 +153,32 @@ export default function mergeDeep(target, source, options = {}) {
|
146 | 153 | let sourceAndTargetTypesMatch = sourceIsArray === targetIsArray;
|
147 | 154 |
|
148 | 155 | if (!sourceAndTargetTypesMatch) {
|
| 156 | + let sourceIsArrayLike = isArrayObject(source); |
| 157 | + |
| 158 | + if (targetIsArray && sourceIsArrayLike) { |
| 159 | + return objectToArray(mergeTargetAndSource(arrayToObject(target), source, options)); |
| 160 | + } |
| 161 | + |
149 | 162 | return source;
|
150 | 163 | } else if (sourceIsArray) {
|
151 | 164 | return source;
|
152 |
| - } |
153 |
| - |
154 |
| - try { |
155 |
| - return mergeTargetAndSource(target, source, options); |
156 |
| - } catch (e) { |
157 |
| - // this is very unlikely to be hit but lets throw an error otherwise |
158 |
| - throw new Error( |
159 |
| - 'Unable to `mergeDeep` with your data. Are you trying to merge two ember-data objects? Please file an issue with ember-changeset.' |
160 |
| - ); |
| 165 | + } else if (target === null || target === undefined) { |
| 166 | + /** |
| 167 | + * If the target was set to null or undefined, we always want to return the source. |
| 168 | + * There is nothing to merge. |
| 169 | + * |
| 170 | + * Without this explicit check, typeof null === typeof {any object-like thing} |
| 171 | + * which means that mergeTargetAndSource will be called, and you can't merge with null |
| 172 | + */ |
| 173 | + return source; |
| 174 | + } else { |
| 175 | + try { |
| 176 | + return mergeTargetAndSource(target, source, options); |
| 177 | + } catch (e) { |
| 178 | + // this is very unlikely to be hit but lets throw an error otherwise |
| 179 | + throw new Error( |
| 180 | + 'Unable to `mergeDeep` with your data. Are you trying to merge two ember-data objects? Please file an issue with ember-changeset.' |
| 181 | + ); |
| 182 | + } |
161 | 183 | }
|
162 | 184 | }
|
0 commit comments