From 9d0215ba32f1e472b98ec11d70ff9d4e0630c647 Mon Sep 17 00:00:00 2001 From: Alexander Sapountzis Date: Fri, 8 Dec 2023 12:52:10 -0500 Subject: [PATCH 1/2] refactor: Replacee individual type checks for customFlagProperty --- src/serverModel.ts | 23 +++++++++-------------- src/utils.ts | 4 ++++ test/src/tests-utils.ts | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/serverModel.ts b/src/serverModel.ts index 744d3a2f..c7dc9e69 100644 --- a/src/serverModel.ts +++ b/src/serverModel.ts @@ -13,7 +13,12 @@ import { SDKProduct, SDKUserIdentity, } from './sdkRuntimeModels'; -import { parseNumber, parseStringOrNumber, Dictionary } from './utils'; +import { + parseNumber, + parseStringOrNumber, + Dictionary, + isValidCustomFlagProperty, +} from './utils'; import { ServerSettings } from './store'; import { MPID } from '@mparticle/web-sdk'; import { @@ -145,22 +150,12 @@ function convertCustomFlags(event: SDKEvent, dto: IServerV2DTO) { if (event.CustomFlags.hasOwnProperty(prop)) { if (Array.isArray(event.CustomFlags[prop])) { - event.CustomFlags[prop].forEach(function(customFlagProperty) { - // TODO: Can we use our utility functions here? - if ( - typeof customFlagProperty === 'number' || - typeof customFlagProperty === 'string' || - typeof customFlagProperty === 'boolean' - ) { + event.CustomFlags[prop].forEach(customFlagProperty => { + if (isValidCustomFlagProperty(customFlagProperty)) { valueArray.push(customFlagProperty.toString()); } }); - } else if ( - // TODO: Can we use our utility functions here? - typeof event.CustomFlags[prop] === 'number' || - typeof event.CustomFlags[prop] === 'string' || - typeof event.CustomFlags[prop] === 'boolean' - ) { + } else if (isValidCustomFlagProperty(convertCustomFlags)) { valueArray.push(event.CustomFlags[prop].toString()); } diff --git a/src/utils.ts b/src/utils.ts index b3df4cde..e9eb8356 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -201,7 +201,10 @@ const converted = (s: string): string => { const isString = (value: any): boolean => typeof value === 'string'; const isNumber = (value: any): boolean => typeof value === 'number'; +const isBoolean = (value: any): boolean => typeof value === 'boolean'; const isFunction = (fn: any): boolean => typeof fn === 'function'; +const isValidCustomFlagProperty = (value: any): boolean => + isNumber(value) || isString(value) || isBoolean(value); const toDataPlanSlug = (value: any): string => // Make sure we are only acting on strings or numbers @@ -247,4 +250,5 @@ export { isFunction, isDataPlanSlug, isEmpty, + isValidCustomFlagProperty, }; diff --git a/test/src/tests-utils.ts b/test/src/tests-utils.ts index 19cf7d59..a07a4ab5 100644 --- a/test/src/tests-utils.ts +++ b/test/src/tests-utils.ts @@ -11,6 +11,7 @@ import { isEmpty, isObject, isStringOrNumber, + isValidCustomFlagProperty, parseNumber, parseStringOrNumber, replaceApostrophesWithQuotes, @@ -352,4 +353,24 @@ describe('Utils', () => { expect(isEmpty(undefined)).to.equal(true); }); }); + + describe('#isValidCustomFlagProperty', () => { + it('returns true if Custom Flag Property is a number', () => { + expect(isValidCustomFlagProperty(42)).to.equal(true); + }); + + it('returns true if Custom Flag Property is a string', () => { + expect(isValidCustomFlagProperty('custom_string')).to.equal(true); + }); + + it('returns true if Custom Flag Property is a boolean', () => { + expect(isValidCustomFlagProperty(true)).to.equal(true); + }); + + it('returns true if Custom Flag Property is not valid', () => { + expect(isValidCustomFlagProperty(null), 'null').to.equal(false); + expect(isValidCustomFlagProperty(function(){}), 'function').to.equal(false); + expect(isValidCustomFlagProperty(undefined), 'undefined').to.equal(false); + }); + }); }); From a0d1d945f67d6d75eb1d7edd2884cbcfc2fe2716 Mon Sep 17 00:00:00 2001 From: Alexander Sapountzis Date: Fri, 8 Dec 2023 13:23:50 -0500 Subject: [PATCH 2/2] Replace incorrect parameter --- src/serverModel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/serverModel.ts b/src/serverModel.ts index c7dc9e69..71cb92be 100644 --- a/src/serverModel.ts +++ b/src/serverModel.ts @@ -155,7 +155,7 @@ function convertCustomFlags(event: SDKEvent, dto: IServerV2DTO) { valueArray.push(customFlagProperty.toString()); } }); - } else if (isValidCustomFlagProperty(convertCustomFlags)) { + } else if (isValidCustomFlagProperty(event.CustomFlags[prop])) { valueArray.push(event.CustomFlags[prop].toString()); }