tuitio-client/src/utils.ts

21 lines
716 B
TypeScript
Raw Normal View History

2023-02-10 01:27:38 +02:00
const combineUrls = (piece1: string, piece2: string): string => {
let baseElement = piece1;
if (baseElement.endsWith("/")) baseElement = baseElement.substring(0, baseElement.length - 1);
return baseElement + piece2;
};
const isValidURL = (str: string): boolean => {
const pattern = new RegExp(
"^(https?:\\/\\/)?" + // protocol
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string
"(\\#[-a-z\\d_]*)?$",
"i"
); // fragment locater
return !!pattern.test(str);
};
export { combineUrls, isValidURL };