Skip to content

Commit

Permalink
feat: add url/cleanPath
Browse files Browse the repository at this point in the history
  • Loading branch information
baxyz committed Dec 20, 2024
1 parent a865d01 commit 1dd92df
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
19 changes: 19 additions & 0 deletions benchmarks/url/cleanPath.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { cleanPath } from 'radashi'

describe('cleanPath', () => {
bench('with no input', () => {
cleanPath(undefined)
})
bench('with empty string', () => {
cleanPath('')
})
bench('with correct path', () => {
cleanPath('/some/path')
})
bench('with multiple slashes in path', () => {
cleanPath('/some//path')
})
bench('with protocol, path, query, and fragment', () => {
cleanPath('https://server//some//path?query=thing#fragment')
})
})
18 changes: 18 additions & 0 deletions docs/url/cleanPath.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: cleanPath
description: Clean a path
---

### Usage

Clean a path by removing duplicate slashes.
The protocol part of the URL is not modified.

```ts
import { cleanPath } from 'radashi'

cleanPath('/path//to///resource') // => '/path/to/resource'
cleanPath('http://example.com//path//to///resource') // => 'http://example.com/path/to/resource'
cleanPath(undefined) // => undefined
cleanPath(null) // => null
```
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export * from './typed/isUndefined.ts'
export * from './typed/isWeakMap.ts'
export * from './typed/isWeakSet.ts'

export * from './url/cleanPath.ts'
export * from './url/onlyPath.ts'
export * from './url/withLeadingSlash.ts'
export * from './url/withoutLeadingSlash.ts'
Expand Down
23 changes: 23 additions & 0 deletions src/url/cleanPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Clean an URL by removing duplicate slashes.
* The protocol part of the URL is not modified.
*
* @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
* @returns The cleaned URL string, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
*
* @example
* ```ts
* cleanPath('/path//to///resource') // => '/path/to/resource'
* cleanPath('http://example.com//path//to///resource') // => 'http://example.com/path/to/resource'
* cleanPath(undefined) // => undefined
* cleanPath(null) // => null
* ```
*/
export function cleanPath(
url: string | undefined | null,
): string | undefined | null {
if (url === undefined || url === null) {
return url
}
return url.replace(/([^:]\/)\/+/g, '$1')
}
29 changes: 29 additions & 0 deletions tests/url/cleanPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { cleanPath } from 'radashi'

describe('cleanPath', () => {
test('should remove duplicate slashes', () => {
expect(cleanPath('/path//to///resource')).toBe('/path/to/resource')
})
test('should handle URLs with protocol correctly', () => {
expect(cleanPath('http://example.com//path//to///resource')).toBe(
'http://example.com/path/to/resource',
)
})
test('should return undefined if input is undefined', () => {
expect(cleanPath(undefined)).toBe(undefined)
})
test('should return null if input is null', () => {
expect(cleanPath(null)).toBe(null)
})
test('should handle path without duplicate slashes', () => {
expect(cleanPath('/path/to/resource')).toBe('/path/to/resource')
})
test('should handle URLs with fragments and queries', () => {
expect(cleanPath('/path//to///resource?query=thing#fragment')).toBe(
'/path/to/resource?query=thing#fragment',
)
expect(
cleanPath('http://example.com//path//to///resource?query=thing#fragment'),
).toBe('http://example.com/path/to/resource?query=thing#fragment')
})
})

0 comments on commit 1dd92df

Please sign in to comment.