Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 52 additions & 46 deletions field-logic/src/lib/flattener.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
import type { SurveyDefinition, UserSession } from '../types/schema';
import type { SurveyDefinition, UserSession } from "../types/schema";

export class SurveyFlattener {
constructor(private definition: SurveyDefinition) { }

generateHeaderMap(): string[] {
// Rule 1: Every node ID becomes a column header
return this.definition.nodes.map(node => node.id);
}

flattenResponse(session: UserSession): any[] {
const headers = this.generateHeaderMap();

return headers.map(nodeId => {
// Logic Check
const visited = session.visitedNodes.includes(nodeId);
const answer = session.responses[nodeId];

if (visited) {
// If visited but no answer, return null (undefined translates to null logic here)
return answer !== undefined ? answer : null;
} else {
// If NOT visited -> Skipped
return 'N/A';
}
});
}

toCSV(sessions: UserSession[]): string {
const headers = this.generateHeaderMap();
const rows = sessions.map(session => this.flattenResponse(session));

const headerRow = headers.join(',');

const dataRows = rows.map(row => {
return row.map((cell: any) => {
if (cell === null || cell === undefined) return '';
const str = String(cell);
// Escape quotes and wrap in quotes if contains comma or newline
if (str.includes(',') || str.includes('\n') || str.includes('"')) {
return `"${str.replace(/"/g, '""')}"`;
}
return str;
}).join(',');
});

return [headerRow, ...dataRows].join('\n');
}
private headers: string[];

constructor(private definition: SurveyDefinition) {
this.headers = this.definition.nodes.map((node) => node.id);
}

generateHeaderMap(): string[] {
// Rule 1: Every node ID becomes a column header
return [...this.headers];
}

flattenResponse(session: UserSession): any[] {
const headers = this.headers;

return headers.map((nodeId) => {
// Logic Check
const visited = session.visitedNodes.includes(nodeId);
const answer = session.responses[nodeId];

if (visited) {
// If visited but no answer, return null (undefined translates to null logic here)
return answer !== undefined ? answer : null;
} else {
// If NOT visited -> Skipped
return "N/A";
}
});
}

toCSV(sessions: UserSession[]): string {
const headers = this.generateHeaderMap();
const rows = sessions.map((session) => this.flattenResponse(session));

const headerRow = headers.join(",");

const dataRows = rows.map((row) => {
return row
.map((cell: any) => {
if (cell === null || cell === undefined) return "";
const str = String(cell);
// Escape quotes and wrap in quotes if contains comma or newline
if (str.includes(",") || str.includes("\n") || str.includes('"')) {
return `"${str.replace(/"/g, '""')}"`;
}
return str;
})
.join(",");
});

return [headerRow, ...dataRows].join("\n");
}
}