Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 29x 2x 27x 1x 26x 18x 8x 8x 8x 3x 5x 5x 5x 4x 5x | /**
* Truncates a string to a word break.
* @param {string} str The string to be truncated.
* @param {number} len The length that the string will be truncated to.
* @param {string} append This is optional.
* @returns {string} The truncated string.
*/
function truncateString(str, len, append = "…") {
let newLength;
// Handle zero or negative length case
if (len <= 0) {
return "";
}
// Handle case where append length is greater than or equal to the max length
if (append.length >= len) {
return str;
}
// If the original string length is less than or equal to the specified length
if (str.length <= len) {
return str;
}
// if the length of original string and the appended string is greater than the max length, we need to truncate, otherwise, use the original string
if (str.length + append.length > len) {
newLength = len - append.length;
} else E{
newLength = str.length;
}
// If there's no space or if the string has no spaces, truncate directly
if (str.indexOf(" ") === -1) {
return str.substring(0, newLength) + append;
}
let tempString = str.substring(0, newLength); //cut the string at the new length
tempString = tempString.replace(/\s+\S*$/, ""); //find the last space that appears before the substringed text
if (append.length > 0) {
tempString += append;
}
return tempString;
}
export { truncateString };
|