mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 17:40:23 +03:00
Created helper function to replace the authority on a URL
This commit is contained in:
parent
4f90d147a4
commit
ace29ca4a4
2 changed files with 20 additions and 0 deletions
7
src/utils/helpers/uri.ts
Normal file
7
src/utils/helpers/uri.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
export const replaceAuthorityFromUri = (uri: string, newAuthority: string): string => {
|
||||||
|
const [ schema, rest ] = uri.split('://');
|
||||||
|
const [ , ...pathParts ] = rest.split('/');
|
||||||
|
const normalizedPath = pathParts.length ? `/${pathParts.join('/')}` : '';
|
||||||
|
|
||||||
|
return `${schema}://${newAuthority}${normalizedPath}`;
|
||||||
|
};
|
13
test/utils/helpers/uri.test.ts
Normal file
13
test/utils/helpers/uri.test.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import { replaceAuthorityFromUri } from '../../../src/utils/helpers/uri';
|
||||||
|
|
||||||
|
describe('uri-helper', () => {
|
||||||
|
describe('replaceAuthorityFromUri', () => {
|
||||||
|
it.each([
|
||||||
|
[ 'http://something.com/foo/bar', 'www.new.to', 'http://www.new.to/foo/bar' ],
|
||||||
|
[ 'https://www.authori.ty:8000/', 'doma.in', 'https://doma.in/' ],
|
||||||
|
[ 'http://localhost:8080/this/is-a-long/path', 'somewhere:8888', 'http://somewhere:8888/this/is-a-long/path' ],
|
||||||
|
])('replaces authority as expected', (uri, newAuthority, expectedResult) => {
|
||||||
|
expect(replaceAuthorityFromUri(uri, newAuthority)).toEqual(expectedResult);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue