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:
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.