Skip to content

Commit

Permalink
fix: load options normalization for boolean values
Browse files Browse the repository at this point in the history
  • Loading branch information
saikumarrs committed Nov 12, 2024
1 parent 783107e commit 87a5b80
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 11 deletions.
102 changes: 102 additions & 0 deletions packages/analytics-js-common/__tests__/utilities/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
removeUndefinedValues,
removeUndefinedAndNullValues,
getNormalizedObjectValue,
getNormalizedBooleanValue,
} from '../../src/utilities/object';

const identifyTraitsPayloadMock = {
Expand Down Expand Up @@ -360,4 +361,105 @@ describe('Common Utils - Object', () => {
});
});
});

describe('getNormalizedBooleanValue', () => {
const tcData = [
{
input: [true, undefined],
output: true,
},
{
input: [false, undefined],
output: false,
},
{
input: [undefined, true],
output: undefined,
},
{
input: [undefined, false],
output: undefined,
},
{
input: [true, false],
output: true,
},
{
input: [false, true],
output: false,
},
{
input: [undefined, undefined],
output: false,
},
{
input: [{}, false],
output: false,
},
{
input: [{}, true],
output: false,
},
{
input: [{}, undefined],
output: false,
},
{
input: [[], false],
output: false,
},
{
input: [[], true],
output: false,
},
{
input: [[], undefined],
output: false,
},
{
input: ['string', false],
output: false,
},
{
input: ['string', true],
output: false,
},
{
input: ['string', undefined],
output: false,
},
{
input: [123456, false],
output: false,
},
{
input: [123456, true],
output: false,
},
{
input: [123456, undefined],
output: false,
},
{
input: [new Date(), false],
output: false,
},
{
input: [new Date(), true],
output: false,
},
{
input: [new Date(), undefined],
output: false,
},
];

it.each(tcData)(
'should return $output for input $input',
({ input, output }: { input: any; output: any }) => {
const outcome = getNormalizedBooleanValue(input[0], input[1]);
expect(outcome).toEqual(output);
},
);
});
});
9 changes: 9 additions & 0 deletions packages/analytics-js-common/src/utilities/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ const getNormalizedObjectValue = (val: any): any => {
return removeUndefinedAndNullValues(val);
};

const getNormalizedBooleanValue = (val: any, defVal: boolean | undefined): boolean | undefined => {
if (isDefined(defVal)) {
return isDefined(val) ? val === true : undefined;
}

return val === true;
};

export {
getValueByPath,
hasValueByPath,
Expand All @@ -119,4 +127,5 @@ export {
removeUndefinedAndNullValues,
getNormalizedObjectValue,
isObject,
getNormalizedBooleanValue,
};
48 changes: 37 additions & 11 deletions packages/analytics-js/src/components/utilities/loadOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable sonarjs/deprecation */
import { clone } from 'ramda';
import {
getNormalizedBooleanValue,
getNormalizedObjectValue,
isNonEmptyObject,
mergeDeepRight,
Expand Down Expand Up @@ -31,9 +32,15 @@ const normalizeLoadOptions = (
normalizedLoadOpts.sameSiteCookie = undefined;
}

normalizedLoadOpts.secureCookie = normalizedLoadOpts.secureCookie === true;
normalizedLoadOpts.secureCookie = getNormalizedBooleanValue(
normalizedLoadOpts.secureCookie,
loadOptionsFromState.secureCookie,
);

normalizedLoadOpts.sameDomainCookiesOnly = normalizedLoadOpts.sameDomainCookiesOnly === true;
normalizedLoadOpts.sameDomainCookiesOnly = getNormalizedBooleanValue(
normalizedLoadOpts.sameDomainCookiesOnly,
loadOptionsFromState.sameDomainCookiesOnly,
);

const uaChTrackLevels = ['none', 'default', 'full'];
if (!uaChTrackLevels.includes(normalizedLoadOpts.uaChTrackLevel as UaChTrackLevel)) {
Expand All @@ -44,15 +51,25 @@ const normalizeLoadOptions = (

normalizedLoadOpts.plugins = normalizedLoadOpts.plugins ?? defaultOptionalPluginsList;

normalizedLoadOpts.useGlobalIntegrationsConfigInEvents =
normalizedLoadOpts.useGlobalIntegrationsConfigInEvents === true;
normalizedLoadOpts.useGlobalIntegrationsConfigInEvents = getNormalizedBooleanValue(
normalizedLoadOpts.useGlobalIntegrationsConfigInEvents,
loadOptionsFromState.useGlobalIntegrationsConfigInEvents,
);

normalizedLoadOpts.bufferDataPlaneEventsUntilReady =
normalizedLoadOpts.bufferDataPlaneEventsUntilReady === true;
normalizedLoadOpts.bufferDataPlaneEventsUntilReady = getNormalizedBooleanValue(
normalizedLoadOpts.bufferDataPlaneEventsUntilReady,
loadOptionsFromState.bufferDataPlaneEventsUntilReady,
);

normalizedLoadOpts.sendAdblockPage = normalizedLoadOpts.sendAdblockPage === true;
normalizedLoadOpts.sendAdblockPage = getNormalizedBooleanValue(
normalizedLoadOpts.sendAdblockPage,
loadOptionsFromState.sendAdblockPage,
);

normalizedLoadOpts.useServerSideCookies = normalizedLoadOpts.useServerSideCookies === true;
normalizedLoadOpts.useServerSideCookies = getNormalizedBooleanValue(
normalizedLoadOpts.useServerSideCookies,
loadOptionsFromState.useServerSideCookies,
);

if (!isString(normalizedLoadOpts.dataServiceEndpoint)) {
normalizedLoadOpts.dataServiceEndpoint = undefined;
Expand All @@ -62,7 +79,10 @@ const normalizeLoadOptions = (
normalizedLoadOpts.sendAdblockPageOptions,
);

normalizedLoadOpts.loadIntegration = normalizedLoadOpts.loadIntegration === true;
normalizedLoadOpts.loadIntegration = getNormalizedBooleanValue(
normalizedLoadOpts.loadIntegration,
loadOptionsFromState.loadIntegration,
);

if (!isNonEmptyObject(normalizedLoadOpts.storage)) {
normalizedLoadOpts.storage = undefined;
Expand All @@ -80,9 +100,15 @@ const normalizeLoadOptions = (

normalizedLoadOpts.queueOptions = getNormalizedObjectValue(normalizedLoadOpts.queueOptions);

normalizedLoadOpts.lockIntegrationsVersion = normalizedLoadOpts.lockIntegrationsVersion === true;
normalizedLoadOpts.lockIntegrationsVersion = getNormalizedBooleanValue(
normalizedLoadOpts.lockIntegrationsVersion,
loadOptionsFromState.lockIntegrationsVersion,
);

normalizedLoadOpts.lockPluginsVersion = normalizedLoadOpts.lockPluginsVersion === true;
normalizedLoadOpts.lockPluginsVersion = getNormalizedBooleanValue(
normalizedLoadOpts.lockPluginsVersion,
loadOptionsFromState.lockPluginsVersion,
);

if (!isNumber(normalizedLoadOpts.dataPlaneEventsBufferTimeout)) {
normalizedLoadOpts.dataPlaneEventsBufferTimeout = undefined;
Expand Down

0 comments on commit 87a5b80

Please sign in to comment.