Initial Commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
export type UseInitialsReturn = {
|
||||
getInitials: (fullName?: string) => string;
|
||||
};
|
||||
|
||||
function getInitial(name: string): string {
|
||||
return Array.from(name)[0] ?? '';
|
||||
}
|
||||
|
||||
export function getInitials(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();
|
||||
}
|
||||
|
||||
return `${getInitial(names[0])}${getInitial(names[names.length - 1])}`.toUpperCase();
|
||||
}
|
||||
|
||||
export function useInitials(): UseInitialsReturn {
|
||||
return { getInitials };
|
||||
}
|
||||
Reference in New Issue
Block a user