* Flattens a record's fields into a plain-text representation. * Each field is rendered as "Field Name: value" on its own line.
( fields: Record<string, unknown>, fieldNames?: Map<string, string> )
| 15 | * Each field is rendered as "Field Name: value" on its own line. |
| 16 | */ |
| 17 | function recordToPlainText( |
| 18 | fields: Record<string, unknown>, |
| 19 | fieldNames?: Map<string, string> |
| 20 | ): string { |
| 21 | const lines: string[] = [] |
| 22 | for (const [key, value] of Object.entries(fields)) { |
| 23 | if (value == null) continue |
| 24 | const displayName = fieldNames?.get(key) ?? key |
| 25 | if (Array.isArray(value)) { |
| 26 | // Attachments or linked records |
| 27 | const items = value.map((v) => { |
| 28 | if (typeof v === 'object' && v !== null) { |
| 29 | const obj = v as Record<string, unknown> |
| 30 | return (obj.url as string) || (obj.name as string) || JSON.stringify(v) |
| 31 | } |
| 32 | return String(v) |
| 33 | }) |
| 34 | lines.push(`${displayName}: ${items.join(', ')}`) |
| 35 | } else if (typeof value === 'object') { |
| 36 | lines.push(`${displayName}: ${JSON.stringify(value)}`) |
| 37 | } else { |
| 38 | lines.push(`${displayName}: ${String(value)}`) |
| 39 | } |
| 40 | } |
| 41 | return lines.join('\n') |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Extracts a human-readable title from a record's fields. |
no test coverage detected