/**
* Flushes output of records for a quick prototype with edge-case handling.
* @param {Array<Object>} records - An array of records to output.
* @param {string} [outputFormat='json'] - The desired output format ('json' or 'text'). Defaults to 'json'.
*/
function flushRecords(records, outputFormat = 'json') {
if (!Array.isArray(records)) {
console.error("Error: records must be an array.");
return; // Exit if not an array
}
if (records.length === 0) {
console.log("No records to output.");
return; // Exit if empty array
}
try {
if (outputFormat === 'json') {
console.log(JSON.stringify(records, null, 2)); // Pretty-print JSON
} else if (outputFormat === 'text') {
for (const record of records) {
console.log(JSON.stringify(record)); // Output each record as JSON string
}
} else {
console.error("Error: Invalid output format. Must be 'json' or 'text'.");
return; // Exit if invalid format
}
} catch (error) {
console.error("Error during output:", error);
}
}
Add your comment