const str= 'I am a very long string thats needs discipline!'
function truncate(str, n) {
return str?.length > n ? str.substr(0, n - 1) + '...' : str;
}
truncate(str,24);
//'I am a very long string...'
The Gist
Pass the string onto the truncate function with the number indicating the the index of the string you want the ellipse(...) truncation to occur.
This versatile function does not only have to be 3 dots you can create 5 dots or any string character for that matter
❤️❤️❤️
Social
Twitter
Linkedin
Portfolio
Github
🤘
Top comments (0)