Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Replace individual type checks for customFlagProperty #813

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(event.CustomFlags[prop])) {
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);
});
});
});
Loading