Skip to content

Conversation

NimeshaKahingala
Copy link
Collaborator

@NimeshaKahingala NimeshaKahingala commented Mar 7, 2025

Refactored File Extension Extraction

  • Improved file extension extraction to handle filenames with multiple dots dynamically.
  • Instead of checking for fixed extensions (.yml or .json) and slicing based on fixed lengths, the new solution uses lastIndexOf('.') to reliably separate the filename and extension.
  • This improves flexibility by supporting any file extension while preventing incorrect slicing for filenames with multiple dots.

Backend Refactor

  • Moved the file extension splitting logic to the backend to keep the frontend logic cleaner and abstract internal implementation details.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think line 16 should have a semicolon appended at the end (;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this modification.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be slightly better if rewritten so that there is only one return statement, with the conditional statement setting the values.

export function getFileNameAndFileType(filename: string): { fileName: string; fileType: string } {
const lastDotIndex = filename.lastIndexOf(".");

const fileName = (lastDotIndex === -1) ? filename : filename.substring(0, lastDotIndex); // Extract name before last dot
const fileType = (lastDotIndex === -1) ? "" : filename.substring(lastDotIndex + 1); // Extract extension after last dot

return { fileName, fileType };

}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did the following modifications.

  • Improved error handling compared to the previous behavior where invalid filenames would silently return an empty file type.
  • Now throws errors for missing or invalid file extensions (e.g., no . or filename ends with a .).

Copy link
Collaborator

@RobertLRead RobertLRead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve this, but can you please make and test the small changes I request?

@RobertLRead RobertLRead merged commit c246a3c into main May 6, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants