| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | const cloneDeep = require('./cloneDeep.js');
|
|---|
| 6 | const isUnsafeProperty = require('../../_internal/isUnsafeProperty.js');
|
|---|
| 7 | const clone = require('../../object/clone.js');
|
|---|
| 8 | const isPrimitive = require('../../predicate/isPrimitive.js');
|
|---|
| 9 | const getSymbols = require('../_internal/getSymbols.js');
|
|---|
| 10 | const isArguments = require('../predicate/isArguments.js');
|
|---|
| 11 | const isArrayLikeObject = require('../predicate/isArrayLikeObject.js');
|
|---|
| 12 | const isObjectLike = require('../predicate/isObjectLike.js');
|
|---|
| 13 | const isPlainObject = require('../predicate/isPlainObject.js');
|
|---|
| 14 | const isTypedArray = require('../predicate/isTypedArray.js');
|
|---|
| 15 |
|
|---|
| 16 | function mergeWith(object, ...otherArgs) {
|
|---|
| 17 | const sources = otherArgs.slice(0, -1);
|
|---|
| 18 | const merge = otherArgs[otherArgs.length - 1];
|
|---|
| 19 | let result = object;
|
|---|
| 20 | for (let i = 0; i < sources.length; i++) {
|
|---|
| 21 | const source = sources[i];
|
|---|
| 22 | result = mergeWithDeep(result, source, merge, new Map());
|
|---|
| 23 | }
|
|---|
| 24 | return result;
|
|---|
| 25 | }
|
|---|
| 26 | function mergeWithDeep(target, source, merge, stack) {
|
|---|
| 27 | if (isPrimitive.isPrimitive(target)) {
|
|---|
| 28 | target = Object(target);
|
|---|
| 29 | }
|
|---|
| 30 | if (source == null || typeof source !== 'object') {
|
|---|
| 31 | return target;
|
|---|
| 32 | }
|
|---|
| 33 | if (stack.has(source)) {
|
|---|
| 34 | return clone.clone(stack.get(source));
|
|---|
| 35 | }
|
|---|
| 36 | stack.set(source, target);
|
|---|
| 37 | if (Array.isArray(source)) {
|
|---|
| 38 | source = source.slice();
|
|---|
| 39 | for (let i = 0; i < source.length; i++) {
|
|---|
| 40 | source[i] = source[i] ?? undefined;
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 | const sourceKeys = [...Object.keys(source), ...getSymbols.getSymbols(source)];
|
|---|
| 44 | for (let i = 0; i < sourceKeys.length; i++) {
|
|---|
| 45 | const key = sourceKeys[i];
|
|---|
| 46 | if (isUnsafeProperty.isUnsafeProperty(key)) {
|
|---|
| 47 | continue;
|
|---|
| 48 | }
|
|---|
| 49 | let sourceValue = source[key];
|
|---|
| 50 | let targetValue = target[key];
|
|---|
| 51 | if (isArguments.isArguments(sourceValue)) {
|
|---|
| 52 | sourceValue = { ...sourceValue };
|
|---|
| 53 | }
|
|---|
| 54 | if (isArguments.isArguments(targetValue)) {
|
|---|
| 55 | targetValue = { ...targetValue };
|
|---|
| 56 | }
|
|---|
| 57 | if (typeof Buffer !== 'undefined' && Buffer.isBuffer(sourceValue)) {
|
|---|
| 58 | sourceValue = cloneDeep.cloneDeep(sourceValue);
|
|---|
| 59 | }
|
|---|
| 60 | if (Array.isArray(sourceValue)) {
|
|---|
| 61 | if (Array.isArray(targetValue)) {
|
|---|
| 62 | const cloned = [];
|
|---|
| 63 | const targetKeys = Reflect.ownKeys(targetValue);
|
|---|
| 64 | for (let i = 0; i < targetKeys.length; i++) {
|
|---|
| 65 | const targetKey = targetKeys[i];
|
|---|
| 66 | cloned[targetKey] = targetValue[targetKey];
|
|---|
| 67 | }
|
|---|
| 68 | targetValue = cloned;
|
|---|
| 69 | }
|
|---|
| 70 | else if (isArrayLikeObject.isArrayLikeObject(targetValue)) {
|
|---|
| 71 | const cloned = [];
|
|---|
| 72 | for (let i = 0; i < targetValue.length; i++) {
|
|---|
| 73 | cloned[i] = targetValue[i];
|
|---|
| 74 | }
|
|---|
| 75 | targetValue = cloned;
|
|---|
| 76 | }
|
|---|
| 77 | else {
|
|---|
| 78 | targetValue = [];
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | const merged = merge(targetValue, sourceValue, key, target, source, stack);
|
|---|
| 82 | if (merged !== undefined) {
|
|---|
| 83 | target[key] = merged;
|
|---|
| 84 | }
|
|---|
| 85 | else if (Array.isArray(sourceValue)) {
|
|---|
| 86 | target[key] = mergeWithDeep(targetValue, sourceValue, merge, stack);
|
|---|
| 87 | }
|
|---|
| 88 | else if (isObjectLike.isObjectLike(targetValue) &&
|
|---|
| 89 | isObjectLike.isObjectLike(sourceValue) &&
|
|---|
| 90 | (isPlainObject.isPlainObject(targetValue) ||
|
|---|
| 91 | isPlainObject.isPlainObject(sourceValue) ||
|
|---|
| 92 | isTypedArray.isTypedArray(targetValue) ||
|
|---|
| 93 | isTypedArray.isTypedArray(sourceValue))) {
|
|---|
| 94 | target[key] = mergeWithDeep(targetValue, sourceValue, merge, stack);
|
|---|
| 95 | }
|
|---|
| 96 | else if (targetValue == null && isPlainObject.isPlainObject(sourceValue)) {
|
|---|
| 97 | target[key] = mergeWithDeep({}, sourceValue, merge, stack);
|
|---|
| 98 | }
|
|---|
| 99 | else if (targetValue == null && isTypedArray.isTypedArray(sourceValue)) {
|
|---|
| 100 | target[key] = cloneDeep.cloneDeep(sourceValue);
|
|---|
| 101 | }
|
|---|
| 102 | else if (targetValue === undefined || sourceValue !== undefined) {
|
|---|
| 103 | target[key] = sourceValue;
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | return target;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | exports.mergeWith = mergeWith;
|
|---|