Skip to content

Instantly share code, notes, and snippets.

@diegorodriguesvieira
Last active July 1, 2020 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegorodriguesvieira/275a4188e3de97bbe3f6e65ff4d3d7c2 to your computer and use it in GitHub Desktop.
Save diegorodriguesvieira/275a4188e3de97bbe3f6e65ff4d3d7c2 to your computer and use it in GitHub Desktop.
Get initials from full name
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