/**
* Aggregates file paths from an array of strings, with defensive checks.
*
* @param {string[]} filePaths An array of file paths to aggregate.
* @returns {string} A single string containing all file paths, separated by commas.
* Returns an empty string if the input is invalid or empty.
*/
function aggregateFilePaths(filePaths) {
if (!Array.isArray(filePaths)) {
console.warn("aggregateFilePaths: Input is not an array.");
return ""; // Return empty string for invalid input
}
if (filePaths.length === 0) {
return ""; // Return empty string if the array is empty
}
let aggregatedPath = "";
for (let i = 0; i < filePaths.length; i++) {
const filePath = filePaths[i];
if (typeof filePath !== 'string') {
console.warn("aggregateFilePaths: Invalid file path found (not a string). Skipping.");
continue; // Skip non-string elements
}
if (!filePath) {
console.warn("aggregateFilePaths: Found empty file path. Skipping.");
continue; //Skip empty strings
}
if (aggregatedPath !== "") {
aggregatedPath += ", "; // Add comma and space if not the first element
}
aggregatedPath += filePath;
}
return aggregatedPath;
}
Add your comment