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

Krishna2323/issue/49286 #813

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion __tests__/Str-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ 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('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 scheme The Scheme to use in the URL
* @returns The formatted URL
*/
sanitizeURL(url: string): string {
sanitizeURL(url: string, scheme = 'https'): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
sanitizeURL(url: string, scheme = 'https'): string {
sanitizeURL(url: string, defaultScheme = 'https'): string {

Copy link
Contributor

@ahmedGaber93 ahmedGaber93 Oct 15, 2024

Choose a reason for hiding this comment

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

I think defaultScheme is more meaningful because it uses only as a default value if the URL didn't have a scheme. We also need to update the comment and its usage.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can also add a unit to confirm that

expect(Str.sanitizeURL('https://example.com', 'http')).toBe('https://example.com');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

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] : `${scheme}://${match[2]}`;
return website.toLowerCase() + this.cutBefore(match[1], match[2]);
},

Expand Down
Loading