Skip to content

Conversation

@PankajBaliyan
Copy link
Contributor

✅ What type of change does this PR introduce?

(Please check all that apply)

  • 🐛 Bug Fix: Fixes a bug or issue.
  • ✨ New Feature: Adds a new feature or functionality.
  • 📚 Snippet Related: Contributes a code snippet.
  • ⚙️ Other (please describe):

📝 Summary of Changes

This pull request introduces a new JavaScript snippet that extracts the file extension from a given URL. It uses the native URL object to parse the input and retrieves the last segment of the path to isolate and return the file extension. If no extension is present or the URL is invalid, it returns null.


🚀 Key Features

  • Parses the input using the URL constructor to ensure robustness
  • Extracts the final path segment to find the file name
  • Handles complex URLs including query strings (e.g., ?download=true)
  • Returns only the last extension (e.g., .gz from .tar.gz)
  • Returns null for invalid URLs or non-file paths
  • Includes console test cases for easy validation

✅ Checklist

  • My code follows the project’s guidelines and formatting rules
  • I’ve thoroughly tested my changes
  • Folder and file names use correct casing
  • This snippet does not duplicate existing functionality
  • Provided a clear description of changes and purpose

📎 Additional Information

  • Contributor: PankajBaliyan
  • Keywords: url, file, extension, extract, path, link

Example usage is included in the snippet for quick testing.

@itsbrunodev itsbrunodev added the snippet Any changes related to a snippet label May 23, 2025
@itsbrunodev
Copy link
Owner

Thanks for submitting this snippet!

@itsbrunodev itsbrunodev merged commit 6d11f95 into itsbrunodev:main May 24, 2025
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

snippet Any changes related to a snippet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants