JSFiddle - React, Tailwind, and code Playground

HTML

<div id="text">

JavaScript

function wordTrim(value, length, overflowSuffix) {
    if (value.length <= length) return value;
    var strAry = value.split(' ');
    var retLen = strAry[0].length;
    for (var i = 1; i < strAry.length; i++) {
        if(retLen == length || retLen + strAry[i].length + 1 > length) break;
        retLen+= strAry[i].length + 1
    }
    return strAry.slice(0,i).join(' ') + (overflowSuffix || '');
}

for(var j = 10; j < 32; j++){
$("#text").append(wordTrim("This is a test of my word trim",j,'...') +  ' <span>' + j + '</span><br>');
}