( path: string, before: string | null, after: string, )
| 15 | } |
| 16 | |
| 17 | export function prepareDiffPreview( |
| 18 | path: string, |
| 19 | before: string | null, |
| 20 | after: string, |
| 21 | ): DiffPreviewPayload { |
| 22 | const beforeBytes = before?.length ?? 0; |
| 23 | const afterBytes = after.length; |
| 24 | const overBudget = beforeBytes > UI_MAX_DIFF_BYTES || afterBytes > UI_MAX_DIFF_BYTES; |
| 25 | |
| 26 | if (!overBudget) { |
| 27 | return { path, before, after }; |
| 28 | } |
| 29 | |
| 30 | // Truncate each side to UI_MAX_DIFF_BYTES, with a marker |
| 31 | const truncate = (s: string | null): string | null => { |
| 32 | if (s === null) return null; |
| 33 | if (s.length <= UI_MAX_DIFF_BYTES) return s; |
| 34 | const half = Math.floor(UI_MAX_DIFF_BYTES / 2); |
| 35 | return s.slice(0, half) + |
| 36 | `\n\n... [${(s.length - UI_MAX_DIFF_BYTES).toLocaleString()} bytes elided for display — full content will be written] ...\n\n` + |
| 37 | s.slice(-half); |
| 38 | }; |
| 39 | |
| 40 | return { |
| 41 | path, |
| 42 | before: truncate(before), |
| 43 | after: truncate(after)!, |
| 44 | truncated: { beforeBytes, afterBytes }, |
| 45 | }; |
| 46 | } |
no test coverage detected