Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/kit/src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,18 @@ export function list_files(dir, filter) {

return files;
}

/**
* Truncates a string in the middle with an ellipsis if it exceeds maxLength.
* @param {string} str
* @param {number} maxLength
* @returns {string}
*/
export function truncate_middle(str, maxLength) {
if (typeof str !== 'string' || str.length <= maxLength || maxLength <= 3) {
return str;
}
const charsToShow = Math.ceil((maxLength - 3) / 2);
const backChars = Math.floor((maxLength - 3) / 2);
return `${str.slice(0, charsToShow)}...${str.slice(str.length - backChars)}`;
}
Loading