Skip to content

Commit a865d01

Browse files
committed
feat: add url/onlyPath
1 parent feb10fe commit a865d01

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed

benchmarks/url/onlyPath.bench.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { onlyPath } from 'radashi'
2+
3+
describe('onlyPath', () => {
4+
bench('with no input', () => {
5+
onlyPath(undefined)
6+
})
7+
bench('with empty string', () => {
8+
onlyPath('')
9+
})
10+
bench('with path only', () => {
11+
onlyPath('/some/path')
12+
})
13+
bench('with path and query', () => {
14+
onlyPath('/some/path?query=thing')
15+
})
16+
bench('with path and fragment', () => {
17+
onlyPath('/some/path#fragment')
18+
})
19+
bench('with path, query, and fragment', () => {
20+
onlyPath('/some/path?query=thing#fragment')
21+
})
22+
})

docs/url/onlyPath.mdx

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: onlyPath
3+
description: Extract only the path
4+
---
5+
6+
### Usage
7+
8+
Extract only the path from an URI with optional query and fragments.
9+
10+
For example, all these parameters will return `/path`:
11+
12+
- `/path`
13+
- `/path?query=thing`
14+
- `/path#fragment`
15+
- `/path?query=thing#fragment`
16+
17+
```ts
18+
import { onlyPath } from 'radashi'
19+
20+
onlyPath('/path') // => '/path'
21+
onlyPath('/path?query=thing') // => '/path'
22+
onlyPath('/path#fragment') // => '/path'
23+
onlyPath('/path?query=thing#fragment') // => '/path'
24+
onlyPath(undefined) // => undefined
25+
onlyPath(null) // => null
26+
```

src/mod.ts

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export * from './typed/isUndefined.ts'
142142
export * from './typed/isWeakMap.ts'
143143
export * from './typed/isWeakSet.ts'
144144

145+
export * from './url/onlyPath.ts'
145146
export * from './url/withLeadingSlash.ts'
146147
export * from './url/withoutLeadingSlash.ts'
147148
export * from './url/withoutTrailingSlash.ts'

src/url/onlyPath.ts

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

tests/url/onlyPath.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { onlyPath } from 'radashi'
2+
3+
describe('onlyPath', () => {
4+
test('should return the path without query and fragment', () => {
5+
expect(onlyPath('/path')).toBe('/path')
6+
})
7+
test('should return the path without query and fragment when query is present', () => {
8+
expect(onlyPath('/path?query=thing')).toBe('/path')
9+
})
10+
test('should return the path without query and fragment when fragment is present', () => {
11+
expect(onlyPath('/path#fragment')).toBe('/path')
12+
})
13+
test('should return the path without query and fragment when both query and fragment are present', () => {
14+
expect(onlyPath('/path?query=thing#fragment')).toBe('/path')
15+
})
16+
test('should return undefined if input is undefined', () => {
17+
expect(onlyPath(undefined)).toBe(undefined)
18+
})
19+
test('should return null if input is null', () => {
20+
expect(onlyPath(null)).toBe(null)
21+
})
22+
})

0 commit comments

Comments
 (0)