( lines: string[], delimiter: string = ',', maxSize: number = MAX_STRING_LENGTH, )
| 96 | * @returns The joined string, truncated if necessary |
| 97 | */ |
| 98 | export function safeJoinLines( |
| 99 | lines: string[], |
| 100 | delimiter: string = ',', |
| 101 | maxSize: number = MAX_STRING_LENGTH, |
| 102 | ): string { |
| 103 | const truncationMarker = '...[truncated]' |
| 104 | let result = '' |
| 105 | |
| 106 | for (const line of lines) { |
| 107 | const delimiterToAdd = result ? delimiter : '' |
| 108 | const fullAddition = delimiterToAdd + line |
| 109 | |
| 110 | if (result.length + fullAddition.length <= maxSize) { |
| 111 | // The full line fits |
| 112 | result += fullAddition |
| 113 | } else { |
| 114 | // Need to truncate |
| 115 | const remainingSpace = |
| 116 | maxSize - |
| 117 | result.length - |
| 118 | delimiterToAdd.length - |
| 119 | truncationMarker.length |
| 120 | |
| 121 | if (remainingSpace > 0) { |
| 122 | // Add delimiter and as much of the line as will fit |
| 123 | result += |
| 124 | delimiterToAdd + line.slice(0, remainingSpace) + truncationMarker |
| 125 | } else { |
| 126 | // No room for any of this line, just add truncation marker |
| 127 | result += truncationMarker |
| 128 | } |
| 129 | return result |
| 130 | } |
| 131 | } |
| 132 | return result |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * A string accumulator that safely handles large outputs by truncating from the end |
no outgoing calls
no test coverage detected