Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute Initials #16

Open
MatthewCallis opened this issue Apr 25, 2017 · 2 comments
Open

Compute Initials #16

MatthewCallis opened this issue Apr 25, 2017 · 2 comments

Comments

@MatthewCallis
Copy link
Owner

    let initials = '';
    if (person.name) {
      initials = person.name.trim().split(' ');
      if (initials.length > 1) {
        initials = `${initials[0][0]}${initials[1][0]}`;
      } else if (initials.length === 1) {
        initials = initials[0][0];
      }
    }
@LydiaMT
Copy link

LydiaMT commented Jun 17, 2021

Accounted for hyphenated last names

function nameInitials(person){
  let initials = '';
    if (person.name) {
      initials = person.name.trim().split(/[ -]/);
      if (initials.length > 2) {
        initials = `${initials[0][0]}${initials[1][0]}${initials[2][0]}`;
        return initials
      } else if(initials.length > 1) {
                initials = `${initials[0][0]}${initials[1][0]}`;
        return initials
      }else if (initials.length === 1) {
        initials = initials[0][0];
        return initials
      }
    }
}
nameInitials({name: "test tester-son"})

@rafpaf
Copy link

rafpaf commented Aug 4, 2021

Another method:

let initials = fullName.split(/[ -]/).map((n) => n.charAt(0)).join('');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants