mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-23 02:08:30 +00:00
Co-authored-by: Mikael Finstad <finstaden@gmail.com> Co-authored-by: Nick Rutten <2504906+nickrttn@users.noreply.github.com> Co-authored-by: Murderlon <merlijn@soverin.net> Co-authored-by: Artur Paikin <artur@arturpaikin.com>
23 lines
924 B
TypeScript
23 lines
924 B
TypeScript
/**
|
|
* Truncates a string to the given number of chars (maxLength) by inserting '...' in the middle of that string.
|
|
* Partially taken from https://stackoverflow.com/a/5723274/3192470.
|
|
*/
|
|
const separator = '...'
|
|
export default function truncateString(
|
|
string: string,
|
|
maxLength: number,
|
|
): string {
|
|
// Return the empty string if maxLength is zero
|
|
if (maxLength === 0) return ''
|
|
// Return original string if it's already shorter than maxLength
|
|
if (string.length <= maxLength) return string
|
|
// Return truncated substring appended of the ellipsis char if string can't be meaningfully truncated
|
|
if (maxLength <= separator.length + 1)
|
|
return `${string.slice(0, maxLength - 1)}…`
|
|
|
|
const charsToShow = maxLength - separator.length
|
|
const frontChars = Math.ceil(charsToShow / 2)
|
|
const backChars = Math.floor(charsToShow / 2)
|
|
|
|
return string.slice(0, frontChars) + separator + string.slice(-backChars)
|
|
}
|