tuitio-client/src/utils.ts

23 lines
753 B
TypeScript

// Copyright (c) 2023 Tudor Stanciu
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 };