truncateStringAtWhitespace
Truncates a string up to specified length, respecting whitespace when possible.
- Determine if
String.prototype.length
is greater or equal tolim
. If not, return it as-is. - Use
String.prototype.slice()
andString.prototype.lastIndexOf()
to find the index of the last space below the desiredlim
. - Use
String.prototype.slice()
to appropriately truncatestr
based onlastSpace
, respecting whitespace if possible and appendingending
at the end. - Omit the third argument,
ending
, to use the default ending of'...'
.