source: node_modules/es-toolkit/dist/object/mergeWith.mjs@ ba17441

Last change on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.3 KB
Line 
1import { isUnsafeProperty } from '../_internal/isUnsafeProperty.mjs';
2import { isPlainObject } from '../predicate/isPlainObject.mjs';
3
4function 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
40export { mergeWith };
Note: See TracBrowser for help on using the repository browser.