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

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

Added visualizations

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