Skip to content

Commit

Permalink
refactor: Replacee individual type checks for customFlagProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle committed Dec 8, 2023
1 parent 6b9f602 commit 9d0215b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/serverModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -247,4 +250,5 @@ export {
isFunction,
isDataPlanSlug,
isEmpty,
isValidCustomFlagProperty,
};
21 changes: 21 additions & 0 deletions test/src/tests-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isEmpty,
isObject,
isStringOrNumber,
isValidCustomFlagProperty,
parseNumber,
parseStringOrNumber,
replaceApostrophesWithQuotes,
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 9d0215b

Please sign in to comment.