Last active
July 1, 2020 22:58
-
-
Save diegorodriguesvieira/275a4188e3de97bbe3f6e65ff4d3d7c2 to your computer and use it in GitHub Desktop.
Get initials from full name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function getNameInitials(name) { | |
if (!name) { | |
return ''; | |
} | |
const initials = name.split(' ').map((item) => item[0]); | |
if (Array.isArray(initials)) { | |
if (initials.length > 2) { | |
return `${initials[0]}${initials[initials.length - 1]}`; | |
} | |
return initials.join(''); | |
} | |
return initials; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment