Skip to content

Commit 2880f72

Browse files
committed
small fixes and tests
1 parent cd577f3 commit 2880f72

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

.size-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ module.exports = [
160160
name: 'CDN Bundle (incl. Tracing)',
161161
path: createCDNPath('bundle.tracing.min.js'),
162162
gzip: true,
163-
limit: '38 KB',
163+
limit: '39 KB',
164164
},
165165
{
166166
name: 'CDN Bundle (incl. Tracing, Replay)',

packages/core/src/utils/merge.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/**
22
* Shallow merge two objects.
33
* Does not mutate the passed in objects.
4+
* Undefined/empty values in the merge object will overwrite existing values.
5+
*
46
* By default, this merges 2 levels deep.
57
*/
68
export function merge<T>(initialObj: T, mergeObj: T, levels = 2): T {
79
// If the merge value is not an object, or we have no merge levels left,
810
// we just set the value to the merge value
9-
if (typeof mergeObj !== 'object' || levels <= 0) {
11+
if (!mergeObj || typeof mergeObj !== 'object' || levels <= 0) {
1012
return mergeObj;
1113
}
1214

packages/core/test/lib/utils/merge.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,25 @@ describe('merge', () => {
5151
expect(actual).not.toBe(oldData);
5252
expect(actual).not.toBe(newData);
5353
});
54+
55+
it.each([
56+
[undefined, { a: 'aa' }, { a: 'aa' }],
57+
[{ a: 'aa' }, undefined, undefined],
58+
[{ a: 'aa' }, null, null],
59+
[{ a: 'aa' }, { a: undefined }, { a: undefined }],
60+
[{ a: 'aa' }, { a: null }, { a: null }],
61+
[{ a: 'aa' }, { a: '' }, { a: '' }],
62+
[
63+
{ a0: { a1: { a2: { a3: { a4: 'a4a' }, a3a: 'a3a' }, a2a: 'a2a' }, a1a: 'a1a' }, a0a: 'a0a' },
64+
{ a0: { a1: { a2: { a3: { a4: 'a4b' }, a3b: 'a3b' }, a2b: 'a2b' }, a1b: 'a1b' }, a0b: 'a0b' },
65+
{
66+
a0: { a1: { a2: { a3: { a4: 'a4a' }, a3b: 'a3a' }, a2b: 'a2b' }, a1b: 'a1b', a1a: 'a1a' },
67+
a0b: 'a0b',
68+
a0a: 'a0a',
69+
},
70+
],
71+
])('works with %p and %p', (oldData, newData, expected) => {
72+
const actual = merge(oldData, newData as any);
73+
expect(actual).toEqual(expected);
74+
});
5475
});

0 commit comments

Comments
 (0)