1. /**
  2. * Aggregates file paths from an array of strings, with defensive checks.
  3. *
  4. * @param {string[]} filePaths An array of file paths to aggregate.
  5. * @returns {string} A single string containing all file paths, separated by commas.
  6. * Returns an empty string if the input is invalid or empty.
  7. */
  8. function aggregateFilePaths(filePaths) {
  9. if (!Array.isArray(filePaths)) {
  10. console.warn("aggregateFilePaths: Input is not an array.");
  11. return ""; // Return empty string for invalid input
  12. }
  13. if (filePaths.length === 0) {
  14. return ""; // Return empty string if the array is empty
  15. }
  16. let aggregatedPath = "";
  17. for (let i = 0; i < filePaths.length; i++) {
  18. const filePath = filePaths[i];
  19. if (typeof filePath !== 'string') {
  20. console.warn("aggregateFilePaths: Invalid file path found (not a string). Skipping.");
  21. continue; // Skip non-string elements
  22. }
  23. if (!filePath) {
  24. console.warn("aggregateFilePaths: Found empty file path. Skipping.");
  25. continue; //Skip empty strings
  26. }
  27. if (aggregatedPath !== "") {
  28. aggregatedPath += ", "; // Add comma and space if not the first element
  29. }
  30. aggregatedPath += filePath;
  31. }
  32. return aggregatedPath;
  33. }

Add your comment