From b021d4567d5c224763c43a9b789fef8979f13186 Mon Sep 17 00:00:00 2001 From: Yatin Date: Sun, 28 Nov 2021 12:13:33 +0530 Subject: [PATCH 1/3] Add ValidateUrl in String --- String/ValidateUrl.js | 14 ++++++++++++++ String/test/ValidateUrl.test.js | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 String/ValidateUrl.js create mode 100644 String/test/ValidateUrl.test.js diff --git a/String/ValidateUrl.js b/String/ValidateUrl.js new file mode 100644 index 0000000000..19dad4d70a --- /dev/null +++ b/String/ValidateUrl.js @@ -0,0 +1,14 @@ +/** + * @function ValidateURL + * @description validate the URL. + * @param {String} url - The input URL string + * @return {Boolean} + * @see [ValidateURL](https://www.w3resource.com/javascript-exercises/javascript-regexp-exercise-9.php) + */ +const validateURL = (url) => { + const URL_PATTERN = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/ + + return URL_PATTERN.test(url) +} + +export { validateURL } diff --git a/String/test/ValidateUrl.test.js b/String/test/ValidateUrl.test.js new file mode 100644 index 0000000000..592143ba04 --- /dev/null +++ b/String/test/ValidateUrl.test.js @@ -0,0 +1,14 @@ +import { validateURL } from '../ValidateUrl' + +describe('ValidateUrl', () => { + it('expects to return false', () => { + expect(validateURL('google')).toEqual(false) + expect(validateURL('htp://www.google.com')).toEqual(false) + }) + + it('expects to return true', () => { + expect(validateURL('http://www.google.com')).toEqual(true) + expect(validateURL('https://www.google.com')).toEqual(true) + expect(validateURL('www.google.com')).toEqual(true) + }) +}) From 804c1e6b2e119aced2d5d7a3d1e95a210701febf Mon Sep 17 00:00:00 2001 From: Yatin Date: Sun, 28 Nov 2021 21:16:57 +0530 Subject: [PATCH 2/3] change regex --- String/ValidateUrl.js | 3 +-- String/test/ValidateUrl.test.js | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/String/ValidateUrl.js b/String/ValidateUrl.js index 19dad4d70a..efd103aeb7 100644 --- a/String/ValidateUrl.js +++ b/String/ValidateUrl.js @@ -3,10 +3,9 @@ * @description validate the URL. * @param {String} url - The input URL string * @return {Boolean} - * @see [ValidateURL](https://www.w3resource.com/javascript-exercises/javascript-regexp-exercise-9.php) */ const validateURL = (url) => { - const URL_PATTERN = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/ + const URL_PATTERN = /(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/gi return URL_PATTERN.test(url) } diff --git a/String/test/ValidateUrl.test.js b/String/test/ValidateUrl.test.js index 592143ba04..d784ff5fc9 100644 --- a/String/test/ValidateUrl.test.js +++ b/String/test/ValidateUrl.test.js @@ -3,7 +3,6 @@ import { validateURL } from '../ValidateUrl' describe('ValidateUrl', () => { it('expects to return false', () => { expect(validateURL('google')).toEqual(false) - expect(validateURL('htp://www.google.com')).toEqual(false) }) it('expects to return true', () => { From 31adf4e38610b73a666281968b3fdfee29e50ad5 Mon Sep 17 00:00:00 2001 From: Yatin Date: Wed, 1 Dec 2021 20:41:26 +0530 Subject: [PATCH 3/3] Bug fix --- String/ValidateUrl.js | 2 +- String/test/ValidateUrl.test.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/String/ValidateUrl.js b/String/ValidateUrl.js index efd103aeb7..cfcbd43666 100644 --- a/String/ValidateUrl.js +++ b/String/ValidateUrl.js @@ -5,7 +5,7 @@ * @return {Boolean} */ const validateURL = (url) => { - const URL_PATTERN = /(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/gi + const URL_PATTERN = /^(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})$/gi return URL_PATTERN.test(url) } diff --git a/String/test/ValidateUrl.test.js b/String/test/ValidateUrl.test.js index d784ff5fc9..e19e46e0f5 100644 --- a/String/test/ValidateUrl.test.js +++ b/String/test/ValidateUrl.test.js @@ -3,6 +3,7 @@ import { validateURL } from '../ValidateUrl' describe('ValidateUrl', () => { it('expects to return false', () => { expect(validateURL('google')).toEqual(false) + expect(validateURL('link: https://www.google.com')).toEqual(false) }) it('expects to return true', () => {