23 lines
631 B
TypeScript
23 lines
631 B
TypeScript
import { useCallback } from 'react';
|
|
|
|
export function useInitials() {
|
|
return useCallback((fullName?: string | null): string => {
|
|
if (!fullName) {
|
|
return '';
|
|
}
|
|
|
|
const names = fullName
|
|
.trim()
|
|
.split(/\s+/u)
|
|
.filter((segment) => segment.length > 0);
|
|
|
|
if (names.length === 0) return '';
|
|
if (names.length === 1) return names[0].charAt(0).toUpperCase();
|
|
|
|
const firstInitial = names[0].charAt(0);
|
|
const lastInitial = names[names.length - 1].charAt(0);
|
|
|
|
return `${firstInitial}${lastInitial}`.toUpperCase();
|
|
}, []);
|
|
}
|