Truncates a string up to a specified length.
String.prototype.length
is greater than num
.'...'
appended to the end or the original string.const truncateString = (str, num) =>
str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
truncateString('boomerang', 7); // 'boom...'
JavaScript, String
Truncates a string up to specified length, respecting whitespace when possible.
JavaScript, String
Generates a random string with the specified length.
JavaScript, String
Pads a string on both sides with the specified character, if it's shorter than the specified length
.