1. function prettyPrintQueue(queueData, environment) {
  2. // Input validation: check if queueData is an array
  3. if (!Array.isArray(queueData)) {
  4. console.error("Error: queueData must be an array.");
  5. return;
  6. }
  7. // Input validation: check if environment is a string
  8. if (typeof environment !== 'string') {
  9. console.error("Error: environment must be a string");
  10. return;
  11. }
  12. console.log(`--- Queue Results for ${environment} ---`);
  13. if (queueData.length === 0) {
  14. console.log("Queue is empty.");
  15. return;
  16. }
  17. queueData.forEach((item, index) => {
  18. // Basic item validation (example: check if item is an object)
  19. if (typeof item !== 'object' || item === null) {
  20. console.warn(`Warning: Invalid item at index ${index}. Skipping. Item type: ${typeof item}`);
  21. return;
  22. }
  23. // Pretty print each item (assuming it's an object)
  24. console.log(`Item ${index + 1}:`);
  25. for (const key in item) {
  26. if (item.hasOwnProperty(key)) {
  27. console.log(` ${key}: ${item[key]}`);
  28. }
  29. }
  30. });
  31. console.log("------------------------------------");
  32. }
  33. // Example Usage (for testing)
  34. const stagingQueue = [
  35. { id: 1, name: "Task A", status: "pending" },
  36. { id: 2, name: "Task B", status: "in progress" },
  37. { id: 3, name: "Task C", status: "completed" }
  38. ];
  39. const productionQueue = [
  40. {id: 101, name: "Prod Task 1", status: "queued"},
  41. {id: 102, name: "Prod Task 2", status: "queued"}
  42. ];
  43. prettyPrintQueue(stagingQueue, "staging");
  44. prettyPrintQueue(productionQueue, "production");
  45. prettyPrintQueue("not an array", "staging"); //Example of invalid input
  46. prettyPrintQueue(stagingQueue, 123); //Example of invalid input

Add your comment