source: node_modules/es-toolkit/dist/object/mergeWith.js@ 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.5 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5const isUnsafeProperty = require('../_internal/isUnsafeProperty.js');
6const isPlainObject = require('../predicate/isPlainObject.js');
7
8function mergeWith(target, source, merge) {
9 const sourceKeys = Object.keys(source);
10 for (let i = 0; i < sourceKeys.length; i++) {
11 const key = sourceKeys[i];
12 if (isUnsafeProperty.isUnsafeProperty(key)) {
13 continue;
14 }
15 const sourceValue = source[key];
16 const targetValue = target[key];
17 const merged = merge(targetValue, sourceValue, key, target, source);
18 if (merged !== undefined) {
19 target[key] = merged;
20 }
21 else if (Array.isArray(sourceValue)) {
22 if (Array.isArray(targetValue)) {
23 target[key] = mergeWith(targetValue, sourceValue, merge);
24 }
25 else {
26 target[key] = mergeWith([], sourceValue, merge);
27 }
28 }
29 else if (isPlainObject.isPlainObject(sourceValue)) {
30 if (isPlainObject.isPlainObject(targetValue)) {
31 target[key] = mergeWith(targetValue, sourceValue, merge);
32 }
33 else {
34 target[key] = mergeWith({}, sourceValue, merge);
35 }
36 }
37 else if (targetValue === undefined || sourceValue !== undefined) {
38 target[key] = sourceValue;
39 }
40 }
41 return target;
42}
43
44exports.mergeWith = mergeWith;
Note: See TracBrowser for help on using the repository browser.