Skip to content

Commit 1dd92df

Browse files
committed
feat: add url/cleanPath
1 parent a865d01 commit 1dd92df

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

benchmarks/url/cleanPath.bench.ts

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

docs/url/cleanPath.mdx

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: cleanPath
3+
description: Clean a path
4+
---
5+
6+
### Usage
7+
8+
Clean a path by removing duplicate slashes.
9+
The protocol part of the URL is not modified.
10+
11+
```ts
12+
import { cleanPath } from 'radashi'
13+
14+
cleanPath('/path//to///resource') // => '/path/to/resource'
15+
cleanPath('http://example.com//path//to///resource') // => 'http://example.com/path/to/resource'
16+
cleanPath(undefined) // => undefined
17+
cleanPath(null) // => null
18+
```

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/cleanPath.ts'
145146
export * from './url/onlyPath.ts'
146147
export * from './url/withLeadingSlash.ts'
147148
export * from './url/withoutLeadingSlash.ts'

src/url/cleanPath.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Clean an URL by removing duplicate slashes.
3+
* The protocol part of the URL is not modified.
4+
*
5+
* @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
6+
* @returns The cleaned URL string, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
7+
*
8+
* @example
9+
* ```ts
10+
* cleanPath('/path//to///resource') // => '/path/to/resource'
11+
* cleanPath('http://example.com//path//to///resource') // => 'http://example.com/path/to/resource'
12+
* cleanPath(undefined) // => undefined
13+
* cleanPath(null) // => null
14+
* ```
15+
*/
16+
export function cleanPath(
17+
url: string | undefined | null,
18+
): string | undefined | null {
19+
if (url === undefined || url === null) {
20+
return url
21+
}
22+
return url.replace(/([^:]\/)\/+/g, '$1')
23+
}

tests/url/cleanPath.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { cleanPath } from 'radashi'
2+
3+
describe('cleanPath', () => {
4+
test('should remove duplicate slashes', () => {
5+
expect(cleanPath('/path//to///resource')).toBe('/path/to/resource')
6+
})
7+
test('should handle URLs with protocol correctly', () => {
8+
expect(cleanPath('http://example.com//path//to///resource')).toBe(
9+
'http://example.com/path/to/resource',
10+
)
11+
})
12+
test('should return undefined if input is undefined', () => {
13+
expect(cleanPath(undefined)).toBe(undefined)
14+
})
15+
test('should return null if input is null', () => {
16+
expect(cleanPath(null)).toBe(null)
17+
})
18+
test('should handle path without duplicate slashes', () => {
19+
expect(cleanPath('/path/to/resource')).toBe('/path/to/resource')
20+
})
21+
test('should handle URLs with fragments and queries', () => {
22+
expect(cleanPath('/path//to///resource?query=thing#fragment')).toBe(
23+
'/path/to/resource?query=thing#fragment',
24+
)
25+
expect(
26+
cleanPath('http://example.com//path//to///resource?query=thing#fragment'),
27+
).toBe('http://example.com/path/to/resource?query=thing#fragment')
28+
})
29+
})

0 commit comments

Comments
 (0)