1. /**
  2. * Deserializes a string of file paths into an array of strings, with defensive checks.
  3. *
  4. * @param {string} filePathString A string containing file paths, separated by commas or spaces.
  5. * @returns {string[]} An array of file paths, or an empty array if input is invalid.
  6. * @throws {TypeError} If input is not a string.
  7. */
  8. function deserializeFilePaths(filePathString) {
  9. if (typeof filePathString !== 'string') {
  10. throw new TypeError('Input must be a string.');
  11. }
  12. if (!filePathString) {
  13. return []; // Return empty array for empty input.
  14. }
  15. // Split the string by commas or spaces.
  16. const filePathArray = filePathString.split(/[,\s]+/).map(filePath => filePath.trim());
  17. // Defensive check: ensure no empty strings after trimming.
  18. const filteredArray = filePathArray.filter(filePath => filePath !== "");
  19. return filteredArray;
  20. }

Add your comment