(parts: Array<string | undefined>, maxLength: number)
| 570 | } |
| 571 | |
| 572 | function compactMetadataDescription(parts: Array<string | undefined>, maxLength: number) { |
| 573 | const compacted = parts |
| 574 | .map((part) => part?.trim()) |
| 575 | .filter((part): part is string => Boolean(part)) |
| 576 | .map(ensureSentence) |
| 577 | .join(" "); |
| 578 | |
| 579 | if (compacted.length <= maxLength) return compacted; |
| 580 | |
| 581 | const shortened = compacted.slice(0, maxLength - 1); |
| 582 | const lastBreak = Math.max( |
| 583 | shortened.lastIndexOf("."), |
| 584 | shortened.lastIndexOf(";"), |
| 585 | shortened.lastIndexOf(","), |
| 586 | ); |
| 587 | const trimmed = (lastBreak > maxLength * 0.6 ? shortened.slice(0, lastBreak) : shortened).trim(); |
| 588 | return `${trimmed.replace(/[.,;:]$/, "")}.`; |
| 589 | } |
| 590 | |
| 591 | function ensureSentence(value: string) { |
| 592 | return /[.!?]$/.test(value) ? value : `${value}.`; |
no outgoing calls
no test coverage detected