({
str,
maxLength,
message = 'TRUNCATED DUE TO LENGTH',
remove = 'END',
}: {
str: string
maxLength: number
message?: string
remove?: 'END' | 'START' | 'MIDDLE'
})
| 8 | } |
| 9 | |
| 10 | export const truncateStringWithMessage = ({ |
| 11 | str, |
| 12 | maxLength, |
| 13 | message = 'TRUNCATED DUE TO LENGTH', |
| 14 | remove = 'END', |
| 15 | }: { |
| 16 | str: string |
| 17 | maxLength: number |
| 18 | message?: string |
| 19 | remove?: 'END' | 'START' | 'MIDDLE' |
| 20 | }) => { |
| 21 | if (str.length <= maxLength) { |
| 22 | return str |
| 23 | } |
| 24 | |
| 25 | if (remove === 'END') { |
| 26 | const suffix = `\n[${message}...]` |
| 27 | return str.slice(0, maxLength - suffix.length) + suffix |
| 28 | } |
| 29 | if (remove === 'START') { |
| 30 | const prefix = `[...${message}]\n` |
| 31 | return prefix + str.slice(str.length - maxLength + prefix.length) |
| 32 | } |
| 33 | |
| 34 | const middle = `\n[...${message}...]\n` |
| 35 | const length = Math.floor((maxLength - middle.length) / 2) |
| 36 | return str.slice(0, length) + middle + str.slice(-length) |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Check if a character is a whitespace character according |
no outgoing calls
no test coverage detected