Skip to content

Commit

Permalink
Merge pull request #795 from callstack-internal/hur/fix-46117
Browse files Browse the repository at this point in the history
  • Loading branch information
francoisl committed Sep 10, 2024
2 parents 9687e27 + 01a0504 commit 6c223d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions __tests__/Str-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,19 @@ describe('Str.isValidE164Phone', () => {
expect(Str.isValidE164Phone('+14404589784')).toBeTruthy();
});
});

describe('Str.getExtension', () => {
it('Correctly returns extension', () => {
expect(Str.getExtension(buildTestURLForType('pdf'))).toBeTruthy();
expect(Str.getExtension(buildTestURLForType('PDF'))).toBeTruthy();
expect(Str.getExtension(buildTestURLForType('mov'))).toBeTruthy();
expect(Str.getExtension(buildTestURLForType('MP4'))).toBeTruthy();
});

it('Returns undefined for data types other than string', () => {
expect(Str.getExtension(0)).toBeUndefined();
expect(Str.getExtension(null)).toBeUndefined();
expect(Str.getExtension(undefined)).toBeUndefined();
expect(Str.getExtension([])).toBeUndefined();
});
});
5 changes: 5 additions & 0 deletions lib/str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as HtmlEntities from 'html-entities';
import * as Constants from './CONST';
import * as UrlPatterns from './Url';
import * as Utils from './utils';
import Log from './Log';

const REMOVE_SMS_DOMAIN_PATTERN = /@expensify\.sms/gi;

Expand Down Expand Up @@ -994,6 +995,10 @@ const Str = {
* without query parameters
*/
getExtension(url: string): string | undefined {
if (typeof url !== 'string') {
Log.warn('Str.getExtension: url is not a string', {url});
return undefined;
}
return url.split('.').pop()?.split('?')[0]?.toLowerCase();
},

Expand Down

0 comments on commit 6c223d9

Please sign in to comment.