Skip to content

Commit

Permalink
Merge pull request #813 from Krishna2323/krishna2323/issue/49286
Browse files Browse the repository at this point in the history
Krishna2323/issue/49286
  • Loading branch information
marcaaron authored Oct 22, 2024
2 parents 68cd6f9 + 1b31c9c commit 2fc4e3b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 3 additions & 1 deletion __tests__/Str-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ describe('Str.sanitizeURL', () => {
expect(Str.sanitizeURL('google.com')).toBe('https://google.com');
expect(Str.sanitizeURL('Https://google.com')).toBe('https://google.com');
expect(Str.sanitizeURL('https://GOOgle.com')).toBe('https://google.com');
expect(Str.sanitizeURL('FOO.com/blah_BLAH')).toBe('https://foo.com/blah_BLAH');
+expect(Str.sanitizeURL('FOO.com/blah_BLAH', 'http')).toBe('http://foo.com/blah_BLAH');
+expect(Str.sanitizeURL('example.com', 'http')).toBe('http://example.com');
expect(Str.sanitizeURL('https://example.com', 'http')).toBe('https://example.com');
expect(Str.sanitizeURL('http://FOO.com/blah_BLAH')).toBe('http://foo.com/blah_BLAH');
expect(Str.sanitizeURL('HTtp://FOO.com/blah_BLAH')).toBe('http://foo.com/blah_BLAH');
});
Expand Down
5 changes: 3 additions & 2 deletions lib/str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -971,15 +971,16 @@ const Str = {
* Formats a URL by converting the domain name to lowercase and adding the missing 'https://' protocol.
*
* @param url The URL to be formatted
* @param defaultScheme The Scheme to use in the URL
* @returns The formatted URL
*/
sanitizeURL(url: string): string {
sanitizeURL(url: string, defaultScheme = 'https'): string {
const regex = new RegExp(`^${UrlPatterns.URL_REGEX}$`, 'i');
const match = regex.exec(url);
if (!match) {
return url;
}
const website = match[3] ? match[2] : `https://${match[2]}`;
const website = match[3] ? match[2] : `${defaultScheme}://${match[2]}`;
return website.toLowerCase() + this.cutBefore(match[1], match[2]);
},

Expand Down

0 comments on commit 2fc4e3b

Please sign in to comment.