mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-13 11:23:50 +00:00
refactor: extract regex utilities
This commit is contained in:
committed by
Carlos Valente
parent
44508ce8b4
commit
5bc286145d
@@ -0,0 +1,45 @@
|
||||
import { checkRegex } from './checkRegex';
|
||||
|
||||
describe('checkRegex()', () => {
|
||||
it('isOnlyNumbers', () => {
|
||||
expect(checkRegex.isOnlyNumbers('12345')).toBe(true);
|
||||
expect(checkRegex.isOnlyNumbers('123a')).toBe(false);
|
||||
});
|
||||
|
||||
it('isIPAddress', () => {
|
||||
expect(checkRegex.isIPAddress('192.168.0.1')).toBe(true);
|
||||
expect(checkRegex.isIPAddress('999.999.999.999')).toBe(false);
|
||||
});
|
||||
|
||||
it('startsWithHttp', () => {
|
||||
expect(checkRegex.startsWithHttp('http://example.com')).toBe(true);
|
||||
expect(checkRegex.startsWithHttp('https://example.com')).toBe(true);
|
||||
expect(checkRegex.startsWithHttp('ftp://example.com')).toBe(false);
|
||||
});
|
||||
|
||||
it('startsWithSlash', () => {
|
||||
expect(checkRegex.startsWithSlash('/path')).toBe(true);
|
||||
expect(checkRegex.startsWithSlash('path')).toBe(false);
|
||||
});
|
||||
|
||||
it('isAlphanumericWithSpace', () => {
|
||||
expect(checkRegex.isAlphanumericWithSpace('abc 123')).toBe(true);
|
||||
expect(checkRegex.isAlphanumericWithSpace('abc-123')).toBe(false);
|
||||
});
|
||||
|
||||
it('isASCII', () => {
|
||||
expect(checkRegex.isASCII('Hello, World!')).toBe(true);
|
||||
expect(checkRegex.isASCII('こんにちは')).toBe(false);
|
||||
});
|
||||
|
||||
it('isASCIIorEmpty', () => {
|
||||
expect(checkRegex.isASCIIorEmpty('')).toBe(true);
|
||||
expect(checkRegex.isASCIIorEmpty('Hello')).toBe(true);
|
||||
expect(checkRegex.isASCIIorEmpty('こんにちは')).toBe(false);
|
||||
});
|
||||
|
||||
it('isNotEmpty', () => {
|
||||
expect(checkRegex.isNotEmpty('Hello')).toBe(true);
|
||||
expect(checkRegex.isNotEmpty('')).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
export const regex = {
|
||||
isOnlyNumbers: /^\d+$/,
|
||||
isIPAddress: /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/,
|
||||
startsWithHttp: /^https?:\/\//,
|
||||
startsWithSlash: /^\//,
|
||||
isAlphanumeric: /^[a-z0-9]+$/i,
|
||||
isAlphanumericWithSpace: /^[a-z0-9_ ]+$/i,
|
||||
isASCII: /^[ -~]+$/, // https://catonmat.net/my-favorite-regex
|
||||
isASCIIorEmpty: /^$|^[ -~]+$/, // https://catonmat.net/my-favorite-regex
|
||||
isNotEmpty: /\S/,
|
||||
isUrlSafe: /^[a-zA-Z0-9_-]*$/, // https://stackoverflow.com/questions/24419067/validate-a-string-to-be-url-safe-using-regex
|
||||
};
|
||||
|
||||
export const checkRegex = {
|
||||
isOnlyNumbers: (text: string): boolean => regex.isOnlyNumbers.test(text),
|
||||
isIPAddress: (text: string): boolean => regex.isIPAddress.test(text),
|
||||
startsWithHttp: (text: string): boolean => regex.startsWithHttp.test(text),
|
||||
startsWithSlash: (text: string): boolean => regex.startsWithSlash.test(text),
|
||||
isAlphanumeric: (text: string): boolean => regex.isAlphanumeric.test(text),
|
||||
isAlphanumericWithSpace: (text: string): boolean => regex.isAlphanumericWithSpace.test(text),
|
||||
isASCII: (text: string): boolean => regex.isASCII.test(text),
|
||||
isASCIIorEmpty: (text: string): boolean => regex.isASCIIorEmpty.test(text),
|
||||
isNotEmpty: (text: string): boolean => regex.isNotEmpty.test(text),
|
||||
isUrlSafe: (text: string): boolean => regex.isUrlSafe.test(text),
|
||||
};
|
||||
@@ -1,17 +0,0 @@
|
||||
import { isAlphanumeric } from './isAlphanumeric';
|
||||
|
||||
describe('test isAlphanumeric() function', () => {
|
||||
it('it OK strings', () => {
|
||||
const ts = ['abcdefghijklmnopqrstuvwxyz', '0123456798', '123asd'];
|
||||
for (const s of ts) {
|
||||
expect(isAlphanumeric(s)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('it bad strings', () => {
|
||||
const ts = ['!abcd1234', 'åøæ', '*'];
|
||||
for (const s of ts) {
|
||||
expect(isAlphanumeric(s)).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,17 +0,0 @@
|
||||
/**
|
||||
* @description Validates a alphanumeric string
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const isAlphanumeric = (text: string): boolean => {
|
||||
const regex = /^[a-z0-9]+$/i;
|
||||
return regex.test(text);
|
||||
};
|
||||
|
||||
/**
|
||||
* @description Validates a alphanumeric string allow space
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const isAlphanumericWithSpace = (text: string): boolean => {
|
||||
const regex = /^[a-z0-9_ ]+$/i;
|
||||
return regex.test(text);
|
||||
};
|
||||
Reference in New Issue
Block a user