JSFiddle - React, Tailwind, and code Playground

by henser

TypeScript

const parseUrl = (url: string): { urlPath: string; search: string } | null => {
    try {
        const { origin, pathname, search } = new URL(url);

        const cleanPathName = pathname.replace(/\/$/, '');

        const urlPath = `${origin}${cleanPathName}`;

        return { urlPath, search };
    } catch (error) {
        console.error('Failed to parse URL', error);
        return null;
    }
};

console.log(parseUrl("   https://ew "));