function truncate (trunc, len) { return truncateEllipses (trunc, len); } function truncateEllipses (trunc, len) { if (trunc.length > len) trunc = trnc(trunc, len, '...'); return trunc } function truncateSquares(trunc, len) { if (trunc.length > len) trunc = trnc(trunc, len, ' [...]'); return trunc } function trnc (trunc, len, chars) { if (trunc.length > len) { /* Truncate the content of the P, then go back to the end of the previous word to ensure that we don't truncate in the middle of a word */ trunc = trunc.substring(0, len); trunc = trunc.replace(/\w+$/, ''); trunc = rtrim(trunc); /* Add an ellipses to the end */ trunc += chars; } return trunc; } function rtrim(s){ return s.replace(/\s\s*$/, ''); }