I am currently trying to create a function that will ellipsize the middle of a text string. The idea is for it to show the first five characters and the last five characters. The middle of the string will just show as
...
So for example if I have dkjkjfdljfldkjfssdfsf
It should show as
dkjkj...sdfsf
I so far have this function:
function ellipsizeIt(text, length) {
if (text.length > length) { return ${text.substring(0, length)}...${text.substring(0, length)}; }
return text;
}
But its just repeating the string first five characters when I do:
ellipsizeIt(text, 5);
It will show as
dkjkj...dkjkj
Any way to do it so that it doesnt just repeat the first five characters in the end of the string and show the actual last 5 characters?
From the code above there is a typo/logic issue where you had ${text.substring(0, length)}...${text.substring(0,length)};.
You are repeating yourself, so is the code.
update your code to
function ellipsizeIt(text, length) {
if (text.length > length) {
return `${text.substring(0, length)}...${text.substring(text.length -length)}`;
}
}