Misc fixes
This commit is contained in:
35
front/src/utils/__tests__/is-domain.test.ts
Normal file
35
front/src/utils/__tests__/is-domain.test.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { isDomain } from '~/utils/is-domain';
|
||||
|
||||
describe('isDomain', () => {
|
||||
it(`should return false if null`, () => {
|
||||
expect(isDomain(null)).toBeFalsy();
|
||||
});
|
||||
|
||||
it(`should return false if undefined`, () => {
|
||||
expect(isDomain(undefined)).toBeFalsy();
|
||||
});
|
||||
|
||||
it(`should return true if string google`, () => {
|
||||
expect(isDomain('google')).toBeFalsy();
|
||||
});
|
||||
|
||||
it(`should return true if string google.com`, () => {
|
||||
expect(isDomain('google.com')).toBeTruthy();
|
||||
});
|
||||
|
||||
it(`should return true if string bbc.co.uk`, () => {
|
||||
expect(isDomain('bbc.co.uk')).toBeTruthy();
|
||||
});
|
||||
|
||||
it(`should return true if string web.io`, () => {
|
||||
expect(isDomain('web.io')).toBeTruthy();
|
||||
});
|
||||
|
||||
it(`should return true if string x.com`, () => {
|
||||
expect(isDomain('x.com')).toBeTruthy();
|
||||
});
|
||||
|
||||
it(`should return true if string 2.com`, () => {
|
||||
expect(isDomain('2.com')).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@ -32,4 +32,12 @@ describe('isURL', () => {
|
||||
it(`should return true if string 2.com`, () => {
|
||||
expect(isURL('2.com')).toBeTruthy();
|
||||
});
|
||||
|
||||
it(`should return true if string https://2.com/test/`, () => {
|
||||
expect(isURL('https://2.com/test/')).toBeTruthy();
|
||||
});
|
||||
|
||||
it(`should return false if string https://2.com/test/sldkfj!?`, () => {
|
||||
expect(isURL('https://2.com/test/sldkfj!?')).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
10
front/src/utils/is-domain.ts
Normal file
10
front/src/utils/is-domain.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { isDefined } from './isDefined';
|
||||
|
||||
export function isDomain(url: string | undefined | null) {
|
||||
return (
|
||||
isDefined(url) &&
|
||||
/^((?!-))(xn--)?[a-z0-9][a-z0-9-_]{0,61}[a-z0-9]{0,1}\.(xn--)?([a-z0-9-]{1,61}|[a-z0-9-]{1,30}\.[a-z]{2,})$/.test(
|
||||
url,
|
||||
)
|
||||
);
|
||||
}
|
||||
@ -1,10 +1,14 @@
|
||||
import { isDefined } from './isDefined';
|
||||
|
||||
export function isURL(url: string | undefined | null) {
|
||||
return (
|
||||
isDefined(url) &&
|
||||
/^((?!-))(xn--)?[a-z0-9][a-z0-9-_]{0,61}[a-z0-9]{0,1}\.(xn--)?([a-z0-9-]{1,61}|[a-z0-9-]{1,30}\.[a-z]{2,})$/.test(
|
||||
url,
|
||||
)
|
||||
const pattern = new RegExp(
|
||||
'^(https?:\\/\\/)?' +
|
||||
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' +
|
||||
'((\\d{1,3}\\.){3}\\d{1,3}))' +
|
||||
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' +
|
||||
'(\\?[;&a-z\\d%_.~+=-]*)?' +
|
||||
'(\\#[-a-z\\d_]*)?$',
|
||||
'i',
|
||||
);
|
||||
return isDefined(url) && !!pattern.test(url);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user