JSFiddle - React, Tailwind, and code Playground

by wookiehangover

HTML

<script src="http://www.broofa.com/Tools/JSLitmus/JSLitmus.js"></script>
<div id="results"/>

JavaScript

function fitBetter(str, n){
    var r = /\W+\w*(?=$)/m,
        s = str.slice(0, n - 1)+"\n";    
    return (str.length <= n) ? str: s.slice(0, s.search(r))+"...";
}

var text = "loremipsum set dolor, est' \"and a; bunch of, other crap",
    i = text.length+2;

while(i--){
    $('#results').append( fitBetter(text, i)+"</br>" );
}



JSLitmus.test('how fast this be', function(){
  
    fitBetter(text, 29);
    
});