useInitials.tsx 482 B

123456789101112131415
  1. import { useCallback } from 'react';
  2. export function useInitials() {
  3. return useCallback((fullName: string): string => {
  4. const names = fullName.trim().split(' ');
  5. if (names.length === 0) return '';
  6. if (names.length === 1) return names[0].charAt(0).toUpperCase();
  7. const firstInitial = names[0].charAt(0);
  8. const lastInitial = names[names.length - 1].charAt(0);
  9. return `${firstInitial}${lastInitial}`.toUpperCase();
  10. }, []);
  11. }