@hackler/react-native-sdk
Version:
React Native SDK for Hackle
44 lines (40 loc) • 1.21 kB
text/typescript
import type { User } from '../models';
export class UserUtil {
static isUserEqual(prevUser?: User | null, currentUser?: User | null) {
return (
prevUser?.id === currentUser?.id &&
prevUser?.deviceId === currentUser?.deviceId &&
prevUser?.userId === currentUser?.userId &&
UserUtil.arePropertiesEqual(
prevUser?.properties,
currentUser?.properties
) &&
UserUtil.arePropertiesEqual(
prevUser?.identifiers,
currentUser?.identifiers
)
);
}
private static arePropertiesEqual(
prevProps: unknown,
currentProps: unknown
): boolean {
const prev = UserUtil.coerceUnknownToProperties(prevProps);
const current = UserUtil.coerceUnknownToProperties(currentProps);
const prevKeys = Object.keys(prev);
const currentKeys = Object.keys(current);
if (prevKeys.length !== currentKeys.length) {
return false;
}
return prevKeys.every((it) => {
// @ts-ignore
return it in current && prev[it] === current[it];
});
}
private static coerceUnknownToProperties(props: unknown) {
if (typeof props === 'object' && props !== null) {
return props;
}
return {};
}
}