source: node_modules/es-toolkit/dist/object/merge.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: 1.0 KB
RevLine 
[a762898]1import { isUnsafeProperty } from '../_internal/isUnsafeProperty.mjs';
2import { isPlainObject } from '../predicate/isPlainObject.mjs';
3
4function merge(target, source) {
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 if (isMergeableValue(sourceValue) && isMergeableValue(targetValue)) {
14 target[key] = merge(targetValue, sourceValue);
15 }
16 else if (Array.isArray(sourceValue)) {
17 target[key] = merge([], sourceValue);
18 }
19 else if (isPlainObject(sourceValue)) {
20 target[key] = merge({}, sourceValue);
21 }
22 else if (targetValue === undefined || sourceValue !== undefined) {
23 target[key] = sourceValue;
24 }
25 }
26 return target;
27}
28function isMergeableValue(value) {
29 return isPlainObject(value) || Array.isArray(value);
30}
31
32export { merge };
Note: See TracBrowser for help on using the repository browser.