Skip to content

Commit 11c8f44

Browse files
committed
turn comments
1 parent 61e1fc5 commit 11c8f44

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

src/roktManager.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ export default class RoktManager {
203203
newHashedEmail = mappedAttributes['emailsha256'] as string || undefined;
204204
}
205205

206-
const emailChanged = !!(newEmail && (!currentEmail || currentEmail !== newEmail));
207-
const hashedEmailChanged = !!(newHashedEmail && (!currentHashedEmail || currentHashedEmail !== newHashedEmail));
208-
// https://go.mparticle.com/work/SQDSDKS-7338
206+
const emailChanged = this.hasIdentityChanged(currentEmail, newEmail);
207+
const hashedEmailChanged = this.hasIdentityChanged(currentHashedEmail, newHashedEmail);
208+
209209
const newIdentities: UserIdentities = {};
210210
if (emailChanged) {
211211
newIdentities.email = newEmail;
@@ -218,6 +218,7 @@ export default class RoktManager {
218218
newIdentities[normalizedHashedEmailUserIdentityType] = newHashedEmail;
219219
this.logger.warning(`emailsha256 mismatch detected. Current mParticle ${normalizedHashedEmailUserIdentityType} identity, ${currentHashedEmail}, differs from from 'emailsha256' passed to selectPlacements call, ${newHashedEmail}. Proceeding to call identify with ${normalizedHashedEmailUserIdentityType} set to ${newHashedEmail}. Please verify your implementation`);
220220
}
221+
221222
if (!isEmpty(newIdentities)) {
222223
// Call identify with the new user identities
223224
try {
@@ -397,4 +398,27 @@ export default class RoktManager {
397398

398399
this.messageQueue.delete(messageId);
399400
}
401+
402+
/**
403+
* Checks if an identity value has changed by comparing current and new values
404+
*
405+
* @param {string | undefined} currentValue - The current identity value
406+
* @param {string | undefined} newValue - The new identity value to compare against
407+
* @returns {boolean} True if the identity has changed (new value exists and differs from current), false otherwise
408+
*/
409+
private hasIdentityChanged(currentValue: string | undefined, newValue: string | undefined): boolean {
410+
if (!newValue) {
411+
return false;
412+
}
413+
414+
if (!currentValue) {
415+
return true; // New value exists but no current value
416+
}
417+
418+
if (currentValue !== newValue) {
419+
return true; // Values are different
420+
}
421+
422+
return false; // Values are the same
423+
}
400424
}

test/jest/roktManager.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,4 +1573,70 @@ describe('RoktManager', () => {
15731573
});
15741574
});
15751575

1576+
describe('#hasIdentityChanged', () => {
1577+
it('should return false when newValue is null', () => {
1578+
const result = roktManager['hasIdentityChanged']('[email protected]', null);
1579+
expect(result).toBe(false);
1580+
});
1581+
1582+
it('should return false when newValue is undefined', () => {
1583+
const result = roktManager['hasIdentityChanged']('[email protected]', undefined);
1584+
expect(result).toBe(false);
1585+
});
1586+
1587+
it('should return false when newValue is empty string', () => {
1588+
const result = roktManager['hasIdentityChanged']('[email protected]', '');
1589+
expect(result).toBe(false);
1590+
});
1591+
1592+
it('should return true when currentValue is null and newValue exists', () => {
1593+
const result = roktManager['hasIdentityChanged'](null, '[email protected]');
1594+
expect(result).toBe(true);
1595+
});
1596+
1597+
it('should return true when currentValue is undefined and newValue exists', () => {
1598+
const result = roktManager['hasIdentityChanged'](undefined, '[email protected]');
1599+
expect(result).toBe(true);
1600+
});
1601+
1602+
it('should return true when currentValue is empty string and newValue exists', () => {
1603+
const result = roktManager['hasIdentityChanged']('', '[email protected]');
1604+
expect(result).toBe(true);
1605+
});
1606+
1607+
it('should return true when currentValue and newValue are different', () => {
1608+
const result = roktManager['hasIdentityChanged']('[email protected]', '[email protected]');
1609+
expect(result).toBe(true);
1610+
});
1611+
1612+
it('should return false when currentValue and newValue are the same', () => {
1613+
const result = roktManager['hasIdentityChanged']('[email protected]', '[email protected]');
1614+
expect(result).toBe(false);
1615+
});
1616+
1617+
it('should return false when both currentValue and newValue are null', () => {
1618+
const result = roktManager['hasIdentityChanged'](null, null);
1619+
expect(result).toBe(false);
1620+
});
1621+
1622+
it('should return false when both currentValue and newValue are undefined', () => {
1623+
const result = roktManager['hasIdentityChanged'](undefined, undefined);
1624+
expect(result).toBe(false);
1625+
});
1626+
1627+
it('should return false when both currentValue and newValue are empty strings', () => {
1628+
const result = roktManager['hasIdentityChanged']('', '');
1629+
expect(result).toBe(false);
1630+
});
1631+
1632+
it('should handle whitespace-only strings as valid values', () => {
1633+
const result = roktManager['hasIdentityChanged']('[email protected]', ' ');
1634+
expect(result).toBe(true);
1635+
});
1636+
1637+
it('should be case sensitive', () => {
1638+
const result = roktManager['hasIdentityChanged']('[email protected]', '[email protected]');
1639+
expect(result).toBe(true);
1640+
});
1641+
});
15761642
});

0 commit comments

Comments
 (0)