-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |