| [a762898] | 1 | import { isUnsafeProperty } from '../_internal/isUnsafeProperty.mjs';
|
|---|
| 2 | import { isPlainObject } from '../predicate/isPlainObject.mjs';
|
|---|
| 3 |
|
|---|
| 4 | function mergeWith(target, source, merge) {
|
|---|
| 5 | const sourceKeys = Object.keys(source);
|
|---|
| 6 | for (let i = 0; i < sourceKeys.length; i++) {
|
|---|
| 7 | const key = sourceKeys[i];
|
|---|
| 8 | if (isUnsafeProperty(key)) {
|
|---|
| 9 | continue;
|
|---|
| 10 | }
|
|---|
| 11 | const sourceValue = source[key];
|
|---|
| 12 | const targetValue = target[key];
|
|---|
| 13 | const merged = merge(targetValue, sourceValue, key, target, source);
|
|---|
| 14 | if (merged !== undefined) {
|
|---|
| 15 | target[key] = merged;
|
|---|
| 16 | }
|
|---|
| 17 | else if (Array.isArray(sourceValue)) {
|
|---|
| 18 | if (Array.isArray(targetValue)) {
|
|---|
| 19 | target[key] = mergeWith(targetValue, sourceValue, merge);
|
|---|
| 20 | }
|
|---|
| 21 | else {
|
|---|
| 22 | target[key] = mergeWith([], sourceValue, merge);
|
|---|
| 23 | }
|
|---|
| 24 | }
|
|---|
| 25 | else if (isPlainObject(sourceValue)) {
|
|---|
| 26 | if (isPlainObject(targetValue)) {
|
|---|
| 27 | target[key] = mergeWith(targetValue, sourceValue, merge);
|
|---|
| 28 | }
|
|---|
| 29 | else {
|
|---|
| 30 | target[key] = mergeWith({}, sourceValue, merge);
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 | else if (targetValue === undefined || sourceValue !== undefined) {
|
|---|
| 34 | target[key] = sourceValue;
|
|---|
| 35 | }
|
|---|
| 36 | }
|
|---|
| 37 | return target;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | export { mergeWith };
|
|---|