add stringwrap by words, limit ani description length

This commit is contained in:
bignutty 2025-01-28 03:20:58 +01:00
parent d58fa1fbe4
commit 19979e440e
3 changed files with 26 additions and 5 deletions

View file

@ -66,7 +66,7 @@ module.exports.timestamp = function(time, flag = "t"){
}
module.exports.stringwrap = function(content = "", length, newlines = true){
if(!newlines) content = content.replace(/\n/, ' ')
if(!newlines) content = content.replace(/\n/g, ' ')
if(content.length > length){
c = content.substring(0, length-1) + '…';
while(c.endsWith(' …')) c = c.substr(0, c.length - 2) + '…';
@ -75,6 +75,27 @@ module.exports.stringwrap = function(content = "", length, newlines = true){
return content;
}
/**
* Limits a string to fit within a certain amount of characters.
* Alternative to {@link stringwrap} that ensures words don't
* get broken up in the middle.
* @param content String Content
* @param length Maximum Length in characters
* @param newlines Remove newlines
* @returns {string} Wrapped String
*/
module.exports.stringwrapPreserveWords = function(content = "", length, newlines = true){
if(!newlines) content = content.replace(/\n/g, ' ')
if(content.length <= length) return content;
content = content.split(" ");
// content size + amount of spaces length + ... char
while(content.join(" ").length + (content.length - 1) > length - 1){
content.pop();
}
return content.join(" ") + "…";
}
module.exports.pill = function(content = ""){
return " **` " + _escapeCodeblock(content) + "  `**"
}