Skip to content

Commit

Permalink
Bugfix: treat strings starting with 'https://' or 'http://' as valid …
Browse files Browse the repository at this point in the history
…URLs (#70)
  • Loading branch information
oliverschwendener committed Dec 12, 2018
1 parent 731943b commit e6db43a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/tests/unit/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export const validUrls = [
"google.com?query=asdf&shit=true",
"mail.google.com",
"https://www.example.com:8080/index.html?p1=A&p2=B#ressource",
"http://goooglecom",
"http://.com",
];

export const invalidUrls = [
Expand All @@ -82,9 +84,7 @@ export const invalidUrls = [
"google.c",
"http:/google.com",
"http//google.com",
"http://goooglecom",
"someone@mail.com",
"http://.com",
"ftp://google.com",
];

Expand Down
7 changes: 5 additions & 2 deletions src/ts/helpers/string-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export class StringHelpers {
}

public static isValidUrl(url: string): boolean {
const regex = new RegExp(/^((https?:)?[/]{2})?([a-z0-9]+[.])+[a-z]{2,}.*$/i, "gi");
return regex.test(url) && !this.isValidEmailAddress(url);
const http = "http://";
const https = "https://";
const fullUrlRegex = new RegExp(/^((https?:)?[/]{2})?([a-z0-9]+[.])+[a-z]{2,}.*$/i, "gi");
const stringStartsWithHttpOrHttps = (url.startsWith(http) && url.length > http.length) || (url.startsWith(https) && url.length > https.length);
return (fullUrlRegex.test(url) || stringStartsWithHttpOrHttps) && !this.isValidEmailAddress(url);
}
}

0 comments on commit e6db43a

Please sign in to comment.