0

I'm trying to create something like this:
https://ui-avatars.com/api/?length=4&name=This%20is%20a%20test%20name%205

Input: This is a test name

Example output:

Example output

I'm not trying to generate images I just need to get initials.

I have tried to do it myself but I just get weird spaces and it's not filling the 4 character spaces.

function getInitials(text) {
    let initials = "";
    let words = text.split(" ");
    words.forEach((word) => {
      switch (words.length) {
        case 1: {
          initials += word + "FFFF";
          break;
        }
        case 2: {
          initials += word.substring(0, 2);
          break;
        }
        case 3: {
          initials += word.substring(0, 2);
          break;
        }
        case 4: {
          initials += word.substring(0, 1);
          break;
        }
        default:
          initials += word.substring(0, 1);
          break;
      }
    });
    return initials.substring(0, 4);
  }

I would appreciate it if someone could help out.

Christian
  • 4,902
  • 4
  • 24
  • 42
  • 1
    `"this is a test name".split(' ').map(x => x.slice(0, 1)).join('')` returns `tiatn`. Is that what you want to do? – Frank Fajardo Sep 08 '22 at 23:23
  • I guess 5 is the length aka number of words with the initials of the first three words only before. – Christian Sep 08 '22 at 23:41
  • It's also a good answer but when I input for example "testing" it would output t but I need it to always be 4 characters. That's the main problem I ran into. – Packet Lightning Sep 08 '22 at 23:45

1 Answers1

0

If you need exact format as in your example, this is an option - just set string and length for result. Check inline comments:

** Updated with new information from comment below:

// Results string length
const length = 4;

// Transform function
const tf = str => {
  // If string is empty or undefined, then generate random string
  if(!str) return (Math.random() + 1).toString(36).substring(2, length+2).toUpperCase();
  
  // Split string to array
  const strArr = str.split(" ");

  // New string
  let newStr = "";
  
  // Check if array larger then or equal length-1
  if(strArr.length >= length-1) {
    // Loop length-1 and extract first letter
    for(let i = 0; i < length-1; i++) {
      if(strArr[i]) newStr += strArr[i].substring(0, 1);
    }
  // If whole string larger then or equal length-1
  } else if(str.length >= length-1) newStr = str.replace(/\s+/g, "").substring(0, length-1);
  // If string length is insufficient, let's repeat pad it
  //else newStr = str.substring(0, 1).repeat(length-1);
  else newStr = str.padStart(length-1, str);

  // Put input string array length at the end of new string
  newStr += strArr.length;

  // Transform to uppercase and return
  return newStr.toUpperCase();
}

// Test result
console.log(tf("This is a test name"));
console.log(tf("Te sting"));
console.log(tf("Go"));
console.log(tf());
tarkh
  • 2,424
  • 1
  • 9
  • 12
  • This is really close to what I needed, but the thing is newStr can be anything it can be one word so when you run it with input "testing" it outputs t1 but I need it to be 4 characters in this case for example in the API thing it would output test when you put testing – Packet Lightning Sep 08 '22 at 23:43
  • Ok, check my updated answer @PacketLightning – tarkh Sep 09 '22 at 00:22
  • That is what I needed I modified it a little bit. Thanks for the help. – Packet Lightning Sep 10 '22 at 10:29