Skip to content

Commit 5b9795d

Browse files
authored
fix: Improve getFileExtension() readability and handle leading dot extensions. (#8980)
1 parent 1c282a3 commit 5b9795d

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/js/utils/url.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,12 @@ export const getAbsoluteURL = function(url) {
4747
*/
4848
export const getFileExtension = function(path) {
4949
if (typeof path === 'string') {
50-
const splitPathRe = /^(\/?)([\s\S]*?)((?:\.{1,2}|[^\/]+?)(\.([^\.\/\?]+)))(?:[\/]*|[\?].*)$/;
51-
const pathParts = splitPathRe.exec(path);
50+
const cleanPath = path.split('?')[0].replace(/\/+$/, '');
5251

53-
if (pathParts) {
54-
return pathParts.pop().toLowerCase();
55-
}
56-
}
52+
const match = cleanPath.match(/\.([^.\/]+)$/);
5753

54+
return match ? match[1].toLowerCase() : '';
55+
}
5856
return '';
5957
};
6058

@@ -74,3 +72,4 @@ export const getFileExtension = function(path) {
7472
export const isCrossOrigin = function(url, winLoc = window.location) {
7573
return parseUrl(url).origin !== winLoc.origin;
7674
};
75+

test/unit/utils/url.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ QUnit.test('should get the file extension of the passed path', function(assert)
5555
assert.equal(Url.getFileExtension('foo/.bar/test.video.flv?foo=bar'), 'flv');
5656
assert.equal(Url.getFileExtension('http://www.test.com/video.mp4'), 'mp4');
5757
assert.equal(Url.getFileExtension('http://foo/bar/test.video.wgg'), 'wgg');
58+
assert.equal(Url.getFileExtension('http://www.test.com/video/.mp4'), 'mp4');
59+
assert.equal(Url.getFileExtension('http://www.test.com/video/.mp4/'), 'mp4');
60+
assert.equal(Url.getFileExtension('http://www.test.com/video.mp4/'), 'mp4');
5861

5962
// edge cases
6063
assert.equal(Url.getFileExtension('http://...'), '');

0 commit comments

Comments
 (0)