Skip to content

Commit e7584f4

Browse files
authored
fix: Correctly merge empty strings in analytics metadata (#105)
1 parent f6b9728 commit e7584f4

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/internal/analytics-metadata/__tests__/metadata-utils.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ describe('merge', () => {
4040
expect(merge(target, source)).toEqual(source);
4141
});
4242
test('recursively merges keys when not defined in nested target', () => {
43-
const source = { one: { two: 'two' } };
43+
const source = { one: { two: 'two', four: '' } };
4444
const target = { one: { three: 'three' } };
45-
expect(merge(target, source)).toEqual({ one: { two: 'two', three: 'three' } });
45+
expect(merge(target, source)).toEqual({ one: { two: 'two', three: 'three', four: '' } });
4646
});
4747
test('copies arrays in target', () => {
4848
const source = { two: 'two' };

src/internal/analytics-metadata/metadata-utils.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export const processMetadata = (node: HTMLElement | null, localMetadata: any): G
2929
}, {});
3030
};
3131

32+
const isNil = (value: any) => {
33+
return typeof value === 'undefined' || value === null;
34+
};
35+
3236
export const merge = (inputTarget: any, inputSource: any): any => {
3337
const merged: any = {};
3438
const target = inputTarget || {};
@@ -39,7 +43,7 @@ export const merge = (inputTarget: any, inputSource: any): any => {
3943
for (const key of allKeys) {
4044
if (target[key] && !source[key]) {
4145
merged[key] = target[key];
42-
} else if (!target[key] && source[key]) {
46+
} else if (!target[key] && !isNil(source[key])) {
4347
merged[key] = source[key];
4448
} else if (typeof target[key] === 'string') {
4549
merged[key] = source[key];

0 commit comments

Comments
 (0)