|
| 1 | +/** |
| 2 | + * Extract only the path from an URI with optional query and fragments. |
| 3 | + * |
| 4 | + * For example, all these parameters will return `/path`: |
| 5 | + * - `/path` |
| 6 | + * - `/path?query=thing` |
| 7 | + * - `/path#fragment` |
| 8 | + * - `/path?query=thing#fragment` |
| 9 | + * |
| 10 | + * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. |
| 11 | + * @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`. |
| 12 | + * |
| 13 | + * @example |
| 14 | + * ```ts |
| 15 | + * onlyPath('/path') // => '/path' |
| 16 | + * onlyPath('/path?query=thing') // => '/path' |
| 17 | + * onlyPath('/path#fragment') // => '/path' |
| 18 | + * onlyPath('/path?query=thing#fragment') // => '/path' |
| 19 | + * onlyPath(undefined) // => undefined |
| 20 | + * onlyPath(null) // => null |
| 21 | + * ``` |
| 22 | + */ |
| 23 | +export function onlyPath(url: string): string |
| 24 | + |
| 25 | +/** |
| 26 | + * Extract only the path from an URI with optional query and fragments. |
| 27 | + * |
| 28 | + * For example, all these parameters will return `/path`: |
| 29 | + * - `/path` |
| 30 | + * - `/path?query=thing` |
| 31 | + * - `/path#fragment` |
| 32 | + * - `/path?query=thing#fragment` |
| 33 | + * |
| 34 | + * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. |
| 35 | + * @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`. |
| 36 | + * |
| 37 | + * @example |
| 38 | + * ```ts |
| 39 | + * onlyPath('/path') // => '/path' |
| 40 | + * onlyPath('/path?query=thing') // => '/path' |
| 41 | + * onlyPath('/path#fragment') // => '/path' |
| 42 | + * onlyPath('/path?query=thing#fragment') // => '/path' |
| 43 | + * onlyPath(undefined) // => undefined |
| 44 | + * onlyPath(null) // => null |
| 45 | + * ``` |
| 46 | + */ |
| 47 | +export function onlyPath(url: null): null |
| 48 | + |
| 49 | +/** |
| 50 | + * Extract only the path from an URI with optional query and fragments. |
| 51 | + * |
| 52 | + * For example, all these parameters will return `/path`: |
| 53 | + * - `/path` |
| 54 | + * - `/path?query=thing` |
| 55 | + * - `/path#fragment` |
| 56 | + * - `/path?query=thing#fragment` |
| 57 | + * |
| 58 | + * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. |
| 59 | + * @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`. |
| 60 | + * |
| 61 | + * @example |
| 62 | + * ```ts |
| 63 | + * onlyPath('/path') // => '/path' |
| 64 | + * onlyPath('/path?query=thing') // => '/path' |
| 65 | + * onlyPath('/path#fragment') // => '/path' |
| 66 | + * onlyPath('/path?query=thing#fragment') // => '/path' |
| 67 | + * onlyPath(undefined) // => undefined |
| 68 | + * onlyPath(null) // => null |
| 69 | + * ``` |
| 70 | + */ |
| 71 | +export function onlyPath(url: undefined): undefined |
| 72 | + |
| 73 | +/** |
| 74 | + * Extract only the path from an URI with optional query and fragments. |
| 75 | + * |
| 76 | + * For example, all these parameters will return `/path`: |
| 77 | + * - `/path` |
| 78 | + * - `/path?query=thing` |
| 79 | + * - `/path#fragment` |
| 80 | + * - `/path?query=thing#fragment` |
| 81 | + * |
| 82 | + * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`. |
| 83 | + * @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`. |
| 84 | + * |
| 85 | + * @example |
| 86 | + * ```ts |
| 87 | + * onlyPath('/path') // => '/path' |
| 88 | + * onlyPath('/path?query=thing') // => '/path' |
| 89 | + * onlyPath('/path#fragment') // => '/path' |
| 90 | + * onlyPath('/path?query=thing#fragment') // => '/path' |
| 91 | + * onlyPath(undefined) // => undefined |
| 92 | + * onlyPath(null) // => null |
| 93 | + * ``` |
| 94 | + */ |
| 95 | +export function onlyPath( |
| 96 | + url: string | undefined | null, |
| 97 | +): string | undefined | null { |
| 98 | + if (url === undefined || url === null) { |
| 99 | + return url |
| 100 | + } |
| 101 | + const [path] = url.split(/[?#]/) |
| 102 | + return path |
| 103 | +} |
0 commit comments