refactor: add centralize avatar initials in DisplayUtils

This commit is contained in:
ajaysehwal
2026-02-18 10:54:09 +05:30
parent 2aa9ac9231
commit e0ed23fc3b
5 changed files with 61 additions and 18 deletions
+2 -2
View File
@@ -2,7 +2,7 @@ import * as React from 'react';
import { Image, Text, View } from 'react-native';
import { themeColor } from '../utils/ThemeUtils';
import { getMintAvatarInitials } from '../utils/CashuUtils';
import { getAvatarInitials } from '../utils/DisplayUtils';
import Ecash from '../assets/images/SVG/Ecash.svg';
@@ -30,7 +30,7 @@ const MintAvatar: React.FC<MintAvatarProps> = ({
const dims = SIZES[size];
if (showPlaceholder) {
const initials = getMintAvatarInitials(name || undefined);
const initials = getAvatarInitials(name || undefined);
return (
<View
style={[
+2 -7
View File
@@ -2,6 +2,7 @@ import { computed } from 'mobx';
import BaseModel from './BaseModel';
import RNFS from 'react-native-fs';
import { getAvatarInitials } from '../utils/DisplayUtils';
import { getPhoto } from '../utils/PhotoUtils';
export default class Contact extends BaseModel {
@@ -184,12 +185,6 @@ export default class Contact extends BaseModel {
}
@computed public get getAvatarInitials(): string {
const name = (this.name || '').trim();
if (!name) return '';
const words = name.split(/\s+/).filter(Boolean);
const firstInitial = words[0][0] || '';
const lastInitial =
words.length > 1 ? words[words.length - 1][0] || '' : '';
return (firstInitial + lastInitial).toUpperCase();
return getAvatarInitials(this.name);
}
}
-9
View File
@@ -90,14 +90,5 @@ class CashuUtils {
};
}
export const getMintAvatarInitials = (name: string | undefined): string => {
const n = (name || '').trim();
if (!n) return '';
const words = n.split(/\s+/).filter(Boolean);
const first = words[0][0] || '';
const last = words.length > 1 ? words[words.length - 1][0] || '' : '';
return (first + last).toUpperCase();
};
const cashuUtils = new CashuUtils();
export default cashuUtils;
+44
View File
@@ -0,0 +1,44 @@
import { getAvatarInitials } from './DisplayUtils';
describe('DisplayUtils', () => {
describe('getAvatarInitials', () => {
it('returns empty string for undefined', () => {
expect(getAvatarInitials(undefined)).toBe('');
});
it('returns empty string for empty string', () => {
expect(getAvatarInitials('')).toBe('');
});
it('returns empty string for whitespace-only string', () => {
expect(getAvatarInitials(' ')).toBe('');
});
it('returns single initial for one word', () => {
expect(getAvatarInitials('John')).toBe('J');
expect(getAvatarInitials('Kaloudis')).toBe('K');
});
it('returns two initials for two words', () => {
expect(getAvatarInitials('Evan Kaloudis')).toBe('EK');
expect(getAvatarInitials('John Doe')).toBe('JD');
});
it('returns first and last initial for three or more words', () => {
expect(getAvatarInitials('John Quincy Adams')).toBe('JA');
expect(getAvatarInitials('Mary Jane Watson Parker')).toBe('MP');
});
it('trims leading and trailing whitespace', () => {
expect(getAvatarInitials(' Evan Kaloudis ')).toBe('EK');
});
it('handles multiple spaces between words', () => {
expect(getAvatarInitials('John Doe')).toBe('JD');
});
it('returns uppercase initials', () => {
expect(getAvatarInitials('john doe')).toBe('JD');
});
});
});
+13
View File
@@ -0,0 +1,13 @@
/**
* Returns avatar initials from a name string.
* Uses first letter of first word and first letter of last word.
* Examples: "Alice Bob" -> "AB", "John Quincy Adams" -> "JA", "John" -> "J"
*/
export const getAvatarInitials = (name: string | undefined): string => {
const n = (name || '').trim();
if (!n) return '';
const words = n.split(/\s+/).filter(Boolean);
const first = words[0][0] || '';
const last = words.length > 1 ? words[words.length - 1][0] || '' : '';
return (first + last).toUpperCase();
};