1. /**
  2. * Flushes output of records for a quick prototype with edge-case handling.
  3. * @param {Array<Object>} records - An array of records to output.
  4. * @param {string} [outputFormat='json'] - The desired output format ('json' or 'text'). Defaults to 'json'.
  5. */
  6. function flushRecords(records, outputFormat = 'json') {
  7. if (!Array.isArray(records)) {
  8. console.error("Error: records must be an array.");
  9. return; // Exit if not an array
  10. }
  11. if (records.length === 0) {
  12. console.log("No records to output.");
  13. return; // Exit if empty array
  14. }
  15. try {
  16. if (outputFormat === 'json') {
  17. console.log(JSON.stringify(records, null, 2)); // Pretty-print JSON
  18. } else if (outputFormat === 'text') {
  19. for (const record of records) {
  20. console.log(JSON.stringify(record)); // Output each record as JSON string
  21. }
  22. } else {
  23. console.error("Error: Invalid output format. Must be 'json' or 'text'.");
  24. return; // Exit if invalid format
  25. }
  26. } catch (error) {
  27. console.error("Error during output:", error);
  28. }
  29. }

Add your comment