dstpabuaran.com/resources/js/hooks/use-initials.tsx
Yoga Pangestu 23cc327190 Refactor code for improved readability and consistency across multiple files
- Adjusted indentation and formatting in login, permissions, profile, and security pages for better readability.
- Enhanced the clarity of conditional statements and function calls in permissions and profile components.
- Updated type definitions in vite-env.d.ts for better code structure.
- Cleaned up array mapping syntax in ProductTest.php for consistency.
2026-08-01 10:14:47 +07:00

31 lines
774 B
TypeScript

import { useCallback } from 'react';
export type GetInitialsFn = (fullName: string) => string;
function getInitial(name: string): string {
return Array.from(name)[0] ?? '';
}
export function useInitials(): GetInitialsFn {
return useCallback((fullName: string): string => {
if (!fullName) {
return '';
}
const names = fullName.trim().split(/\s+/u).filter(Boolean);
if (names.length === 0) {
return '';
}
if (names.length === 1) {
return getInitial(names[0]).toUpperCase();
}
const firstInitial = getInitial(names[0]);
const lastInitial = getInitial(names[names.length - 1]);
return `${firstInitial}${lastInitial}`.toUpperCase();
}, []);
}