/**
* Deserializes a string of file paths into an array of strings, with defensive checks.
*
* @param {string} filePathString A string containing file paths, separated by commas or spaces.
* @returns {string[]} An array of file paths, or an empty array if input is invalid.
* @throws {TypeError} If input is not a string.
*/
function deserializeFilePaths(filePathString) {
if (typeof filePathString !== 'string') {
throw new TypeError('Input must be a string.');
}
if (!filePathString) {
return []; // Return empty array for empty input.
}
// Split the string by commas or spaces.
const filePathArray = filePathString.split(/[,\s]+/).map(filePath => filePath.trim());
// Defensive check: ensure no empty strings after trimming.
const filteredArray = filePathArray.filter(filePath => filePath !== "");
return filteredArray;
}
Add your comment