Open History to see the last commit
1import { type UserInputAttachmentAnswerPayload } from "@t3tools/contracts";
2
3function record(value: unknown): Record<string, unknown> | undefined {
4 return value !== null && typeof value === "object" && !Array.isArray(value)
5 ? (value as Record<string, unknown>)
6 : undefined;
7}
8
9export function getQuestionAnswerText(value: unknown): string {
10 if (typeof value === "string") return value;
11 if (Array.isArray(value)) return value.map(getQuestionAnswerText).filter(Boolean).join(", ");
12 const nested = record(value);
13 return nested ? getQuestionAnswerText(nested.answers) : "";
14}
15
16export function getQuestionAnswerPreview(answer: UserInputAttachmentAnswerPayload): string {
17 const answers = Object.values(answer.answers).map(getQuestionAnswerText).filter(Boolean);
18 const attachments = Object.values(answer.attachmentsByQuestionId)
19 .flat()
20 .map((attachment) => attachment.name);
21 return (
22 answers.length > 0
23 ? answers.join(" · ")
24 : attachments.length > 0
25 ? attachments.join(", ")
26 : Object.values(answer.questionTextById ?? {}).join(" · ")
27 )
28 .replace(/\s+/g, " ")
29 .trim();
30}
31
32export function hasQuestionAnswer(answer: UserInputAttachmentAnswerPayload): boolean {
33 return (
34 Object.values(answer.answers).some(getQuestionAnswerText) ||
35 Object.values(answer.attachmentsByQuestionId).some((attachments) => attachments.length > 0)
36 );
37}