diff --git a/app/components/UI/NetworkModal/__snapshots__/index.test.tsx.snap b/app/components/UI/NetworkModal/__snapshots__/index.test.tsx.snap index c6973deb960..604755ff9fe 100644 --- a/app/components/UI/NetworkModal/__snapshots__/index.test.tsx.snap +++ b/app/components/UI/NetworkModal/__snapshots__/index.test.tsx.snap @@ -425,7 +425,7 @@ exports[`NetworkDetails renders correctly 1`] = ` } } > - https:/ + https://localhost:8545 - http:/ + http://test.com - https:/ + https://evm.cronos.org { const sanitizedUrl = hideKeyFromUrl(urlString); expect(sanitizedUrl).toEqual(undefined); }); + + it('should not hide key from url', () => { + const urlString = 'https://www.example.com'; + const sanitizedUrl = hideKeyFromUrl(urlString); + expect(sanitizedUrl).toEqual('https://www.example.com'); + }); + + it('should hide key from url if protocol is not defined', () => { + const urlString = 'www.example.com/v1/1234'; + const sanitizedUrl = hideKeyFromUrl(urlString); + expect(sanitizedUrl).toEqual('www.example.com/v1'); + }); }); diff --git a/app/util/hideKeyFromUrl.ts b/app/util/hideKeyFromUrl.ts index 7a196758986..29a4c915d68 100644 --- a/app/util/hideKeyFromUrl.ts +++ b/app/util/hideKeyFromUrl.ts @@ -1,4 +1,19 @@ -const hideKeyFromUrl = (url: string | undefined) => - url?.substring(0, url.lastIndexOf('/')); +const hideKeyFromUrl = (url: string | undefined) => { + if (!url) return url; + + const regex = /^(https?:\/\/)(.*)$/; + const match = url.match(regex); + + if (match) { + const protocol = match[1]; + let restOfUrl = match[2]; + + // eslint-disable-next-line no-useless-escape + restOfUrl = restOfUrl.replace(/\/[^\/]*$/, ''); + return protocol + restOfUrl; + } + + return url?.substring(0, url.lastIndexOf('/')); +}; export default hideKeyFromUrl;