Skip to content

Commit 742d57e

Browse files
Address PR Comments
1 parent 04dff63 commit 742d57e

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

src/apiClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export default function APIClient(
135135
this.queueEventForBatchUpload(event);
136136
}
137137

138+
// https://go.mparticle.com/work/SQDSDKS-6935
138139
// While Event Name is 'usually' a string, there are some cases where it is a number
139140
// in that it could be a type of MessageType Enum
140141
if (event.EventName as unknown as number !== Types.MessageType.AppStateTransition) {

src/types.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Constants from './constants';
2-
import { parseNumber, valueof } from './utils';
2+
import { isNumber, parseNumber, valueof } from './utils';
33

44
interface IdentitiesByType {
55
[key: number]: string;
@@ -159,7 +159,9 @@ export const IdentityType = {
159159
}
160160
},
161161

162-
getIdentityType: (identityName: string): valueof<typeof IdentityType> | boolean => {
162+
getIdentityType: (
163+
identityName: string
164+
): valueof<typeof IdentityType> | boolean => {
163165
switch (identityName) {
164166
case 'other':
165167
return IdentityType.Other;
@@ -208,7 +210,7 @@ export const IdentityType = {
208210
}
209211
},
210212

211-
getIdentityName: (identityType): string | null => {
213+
getIdentityName: (identityType: number): string | null => {
212214
switch (identityType) {
213215
case IdentityType.Other:
214216
return 'other';
@@ -257,14 +259,25 @@ export const IdentityType = {
257259
}
258260
},
259261

260-
getNewIdentitiesByName: (newIdentitiesByType: IdentitiesByType): IdentitiesByType => {
262+
// Strips out functions from Identity Types for easier lookups
263+
getValuesAsStrings: (): string[] =>
264+
Object.values(IdentityType)
265+
.map(value => (isNumber(value) ? value.toString() : undefined))
266+
.filter(value => value !== undefined) as string[],
267+
268+
getNewIdentitiesByName: (
269+
newIdentitiesByType: IdentitiesByType
270+
): IdentitiesByType => {
261271
const newIdentitiesByName: IdentitiesByType = {};
262272

263273
for (const key in newIdentitiesByType) {
264-
const identityNameKey = IdentityType.getIdentityName(
265-
parseNumber(key)
266-
);
267-
newIdentitiesByName[identityNameKey] = newIdentitiesByType[key];
274+
// IdentityTypes are stored as numbers but are passed in as strings
275+
if (IdentityType.getValuesAsStrings().includes(key)) {
276+
const identityNameKey = IdentityType.getIdentityName(
277+
parseNumber(key)
278+
);
279+
newIdentitiesByName[identityNameKey] = newIdentitiesByType[key];
280+
}
268281
}
269282

270283
return newIdentitiesByName;

test/jest/types.spec.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,36 @@ describe('IdentityType', () => {
238238
expect(PhoneNumber3).toEqual(21);
239239
});
240240

241+
describe('#getValuesAsStrings', () => {
242+
it('returns an array of identity types values as an array of numbers as strings', () => {
243+
const { getValuesAsStrings } = IdentityType;
244+
245+
expect(getValuesAsStrings()).toEqual([
246+
'0',
247+
'1',
248+
'2',
249+
'3',
250+
'4',
251+
'5',
252+
'6',
253+
'7',
254+
'9',
255+
'10',
256+
'11',
257+
'12',
258+
'13',
259+
'14',
260+
'15',
261+
'16',
262+
'17',
263+
'18',
264+
'19',
265+
'20',
266+
'21',
267+
]);
268+
});
269+
});
270+
241271
describe('#isValid', () => {
242272
it('returns true if the identity type is valid', () => {
243273
const { isValid } = IdentityType;
@@ -288,9 +318,7 @@ describe('IdentityType', () => {
288318
});
289319
});
290320

291-
// FIXME: We have a bug in the implementation
292-
// FIXME: parseNumber causes unknown identity type to be returned as 0
293-
it.skip('returns an empty object if the identity type is not found', () => {
321+
it('returns an empty object if the identity type is not found', () => {
294322
const { getNewIdentitiesByName } = IdentityType;
295323

296324
const newIdentitiesByType = {

0 commit comments

Comments
 (0)