( absoluteUrl: string, result: 'relative' | 'pathname' | 'hash' = 'relative', )
| 9 | import {normalizePath} from './navigation.utils'; |
| 10 | |
| 11 | export function getRelativeUrl( |
| 12 | absoluteUrl: string, |
| 13 | result: 'relative' | 'pathname' | 'hash' = 'relative', |
| 14 | ): string { |
| 15 | const url = new URL(normalizePath(absoluteUrl)); |
| 16 | |
| 17 | if (result === 'hash') { |
| 18 | return url.hash?.substring(1) ?? ''; |
| 19 | } |
| 20 | if (result === 'pathname') { |
| 21 | return `${removeTrailingSlash(normalizePath(url.pathname))}`; |
| 22 | } |
| 23 | return `${removeTrailingSlash(normalizePath(url.pathname))}${url.hash ?? ''}`; |
| 24 | } |
| 25 | |
| 26 | export const removeTrailingSlash = (url: string): string => { |
| 27 | if (url.endsWith('/')) { |
no test coverage detected
searching dependent graphs…