Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getFileExtension() to Improve Readability and Handle Edge Cases. #8980

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

damanV5
Copy link

@damanV5 damanV5 commented Feb 19, 2025

Description

More robust edge case handling for getFileExtension().

Case : Type is not provided explicitly in {src,type} object.
In this case getFileExtension() is called to fetch the extension so as to get the type.

For certain cases, fn does not returns the correct extension.
For Eg:

  1. https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8
  2. https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8/

In this case fn returns "".
I have simplified getFileExtension() so that fn will return m3u8 in this case as well.

Specific Changes proposed

Simplified getFileExtension() for better readability and to handle more edge cases.

Requirements Checklist

  • Feature implemented / Bug fixed
  • If necessary, more likely in a feature request than a bug fix
    • Change has been verified in an actual browser (Chrome, Firefox, IE)
    • Unit Tests updated or fixed
    • Docs/guides updated
    • Example created (starter template on JSBin)
    • Has no DOM changes which impact accessiblilty or trigger warnings (e.g. Chrome issues tab)
    • Has no changes to JSDoc which cause npm run docs:api to error
  • Reviewed by Two Core Contributors

Copy link

welcome bot commented Feb 19, 2025

💖 Thanks for opening this pull request! 💖

Things that will help get your PR across the finish line:

  • Run npm run lint -- --errors locally to catch formatting errors earlier.
  • Include tests when adding/changing behavior.
  • Include screenshots and animated GIFs whenever possible.

We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can.

@damanV5 damanV5 changed the title fix: getFileExtension to accept a trailing slash fix: modified regex in getFileExtension() to accept a trailing slash Feb 19, 2025
@damanV5 damanV5 changed the title fix: modified regex in getFileExtension() to accept a trailing slash fix: modified getFileExtension() to accept a trailing slash Feb 19, 2025
@damanV5
Copy link
Author

damanV5 commented Feb 19, 2025

@gkatsev , @heff , @mister-ben Could you please help with getting this merged?

Copy link

codecov bot commented Feb 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.16%. Comparing base (8842d37) to head (86053c0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8980      +/-   ##
==========================================
+ Coverage   84.13%   84.16%   +0.03%     
==========================================
  Files         120      120              
  Lines        8117     8116       -1     
  Branches     1948     1948              
==========================================
+ Hits         6829     6831       +2     
+ Misses       1288     1285       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@mister-ben mister-ben left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! It's a nice simplification. It would consider com in https://example.com a file extensions too, but so does the original.

Could you add a test to test/until/utils/url.test.js with the type of URL that should now work?

I don't think "accept a training slash" is the best description. When I read that I thought it's intended for a URL like .../file.m3u8/. Something like to "match an extension without a filename"?

@damanV5
Copy link
Author

damanV5 commented Feb 20, 2025

Sure @mister-ben . Let me add a test for this.
FYR: https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8 : This will be the case which will now be handled.

I agree accept a training slash is not the best description, let me update that as well.

@damanV5 damanV5 changed the title fix: modified getFileExtension() to accept a trailing slash fix: getFileExtension() to Improve Readability and Handle Edge Cases. Feb 20, 2025
@damanV5
Copy link
Author

damanV5 commented Feb 20, 2025

@mister-ben I have added test cases in test/until/utils/url.test.js and updated the PR description as well.
Let me know if something else needs to be done.

@damanV5 damanV5 requested a review from mister-ben February 21, 2025 07:29
@@ -55,6 +55,8 @@ QUnit.test('should get the file extension of the passed path', function(assert)
assert.equal(Url.getFileExtension('foo/.bar/test.video.flv?foo=bar'), 'flv');
assert.equal(Url.getFileExtension('http://www.test.com/video.mp4'), 'mp4');
assert.equal(Url.getFileExtension('http://foo/bar/test.video.wgg'), 'wgg');
assert.equal(Url.getFileExtension('http://www.test.com/video/.mp4'), 'mp4');
assert.equal(Url.getFileExtension('http://www.test.com/video/.mp4/'), 'mp4');
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, my earlier comment wasn't to suggest we should support this trailing slash, just that the previous title sounded like this is was the PR was intended to do. I don't think we should add this if there is no precedent for it. The regex as it was before was good.

Copy link
Author

@damanV5 damanV5 Feb 21, 2025

Choose a reason for hiding this comment

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

@mister-ben The updated regex will also handle a trailing slash , which was not handled before.

As per your comment, I thought it would be great to handle trailing slash as well.

Eg. For urls like: test.com/mp4/ : it was previously returning "".

So now, this update
will return mp4 for this.

Though, let me know I can revert it if that is not required.

Copy link
Author

@damanV5 damanV5 Feb 21, 2025

Choose a reason for hiding this comment

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

I just checked the original regex also handled the trailing slash, but there was no test case written for that.
So I believe, we should keep supporting the trailing slash case.

Apologies for the confusion, but I'd like to clarify once more.

With this PR, the following case will now be handled :-
https://demo.unified-streaming.com/tears-of-steel.ism/.m3u8

Original Regex was returning "" for this particular case.

Summary:-
This PR is an attempt to handle more edge cases like above and improves the readability and simplicity of the getFileExtension() function.

Thanks !

@damanV5 damanV5 requested a review from mister-ben February 21, 2025 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants