From 6fb649e53d9e19f7357e61cc8ad63a258013d009 Mon Sep 17 00:00:00 2001 From: YATIN KATHURIA <47096348+Yatin-kathuria@users.noreply.github.com> Date: Thu, 2 Dec 2021 16:23:54 +0530 Subject: [PATCH] merge: Add ValidateUrl in String (#856) * Add ValidateUrl in String * change regex * Bug fix --- String/ValidateUrl.js | 13 +++++++++++++ String/test/ValidateUrl.test.js | 14 ++++++++++++++ 2 files changed, 27 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 000000000..cfcbd4366 --- /dev/null +++ b/String/ValidateUrl.js @@ -0,0 +1,13 @@ +/** + * @function ValidateURL + * @description validate the URL. + * @param {String} url - The input URL string + * @return {Boolean} + */ +const validateURL = (url) => { + const URL_PATTERN = /^(https?:\/\/(?:www\.|(?!www))[^\s.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})$/gi + + 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 000000000..e19e46e0f --- /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('link: https://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) + }) +})