merge: Add ValidateUrl in String (#856)

* Add ValidateUrl in String

* change regex

* Bug fix
This commit is contained in:
YATIN KATHURIA
2021-12-02 16:23:54 +05:30
committed by GitHub
parent 961f21f97c
commit 6fb649e53d
2 changed files with 27 additions and 0 deletions

13
String/ValidateUrl.js Normal file
View File

@ -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 }

View File

@ -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)
})
})