Skip to content

Commit

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

describe('onlyPath', () => {
bench('with no input', () => {
onlyPath(undefined)
})
bench('with empty string', () => {
onlyPath('')
})
bench('with path only', () => {
onlyPath('/some/path')
})
bench('with path and query', () => {
onlyPath('/some/path?query=thing')
})
bench('with path and fragment', () => {
onlyPath('/some/path#fragment')
})
bench('with path, query, and fragment', () => {
onlyPath('/some/path?query=thing#fragment')
})
})
26 changes: 26 additions & 0 deletions docs/url/onlyPath.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: onlyPath
description: Extract only the path
---

### Usage

Extract only the path from an URI with optional query and fragments.

For example, all these parameters will return `/path`:

- `/path`
- `/path?query=thing`
- `/path#fragment`
- `/path?query=thing#fragment`

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

onlyPath('/path') // => '/path'
onlyPath('/path?query=thing') // => '/path'
onlyPath('/path#fragment') // => '/path'
onlyPath('/path?query=thing#fragment') // => '/path'
onlyPath(undefined) // => undefined
onlyPath(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/onlyPath.ts'
export * from './url/withLeadingSlash.ts'
export * from './url/withoutLeadingSlash.ts'
export * from './url/withoutTrailingSlash.ts'
Expand Down
103 changes: 103 additions & 0 deletions src/url/onlyPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Extract only the path from an URI with optional query and fragments.
*
* For example, all these parameters will return `/path`:
* - `/path`
* - `/path?query=thing`
* - `/path#fragment`
* - `/path?query=thing#fragment`
*
* @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
* @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
*
* @example
* ```ts
* onlyPath('/path') // => '/path'
* onlyPath('/path?query=thing') // => '/path'
* onlyPath('/path#fragment') // => '/path'
* onlyPath('/path?query=thing#fragment') // => '/path'
* onlyPath(undefined) // => undefined
* onlyPath(null) // => null
* ```
*/
export function onlyPath(url: string): string

/**
* Extract only the path from an URI with optional query and fragments.
*
* For example, all these parameters will return `/path`:
* - `/path`
* - `/path?query=thing`
* - `/path#fragment`
* - `/path?query=thing#fragment`
*
* @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
* @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
*
* @example
* ```ts
* onlyPath('/path') // => '/path'
* onlyPath('/path?query=thing') // => '/path'
* onlyPath('/path#fragment') // => '/path'
* onlyPath('/path?query=thing#fragment') // => '/path'
* onlyPath(undefined) // => undefined
* onlyPath(null) // => null
* ```
*/
export function onlyPath(url: null): null

/**
* Extract only the path from an URI with optional query and fragments.
*
* For example, all these parameters will return `/path`:
* - `/path`
* - `/path?query=thing`
* - `/path#fragment`
* - `/path?query=thing#fragment`
*
* @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
* @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
*
* @example
* ```ts
* onlyPath('/path') // => '/path'
* onlyPath('/path?query=thing') // => '/path'
* onlyPath('/path#fragment') // => '/path'
* onlyPath('/path?query=thing#fragment') // => '/path'
* onlyPath(undefined) // => undefined
* onlyPath(null) // => null
* ```
*/
export function onlyPath(url: undefined): undefined

/**
* Extract only the path from an URI with optional query and fragments.
*
* For example, all these parameters will return `/path`:
* - `/path`
* - `/path?query=thing`
* - `/path#fragment`
* - `/path?query=thing#fragment`
*
* @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
* @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
*
* @example
* ```ts
* onlyPath('/path') // => '/path'
* onlyPath('/path?query=thing') // => '/path'
* onlyPath('/path#fragment') // => '/path'
* onlyPath('/path?query=thing#fragment') // => '/path'
* onlyPath(undefined) // => undefined
* onlyPath(null) // => null
* ```
*/
export function onlyPath(
url: string | undefined | null,
): string | undefined | null {
if (url === undefined || url === null) {
return url
}
const [path] = url.split(/[?#]/)
return path
}
22 changes: 22 additions & 0 deletions tests/url/onlyPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { onlyPath } from 'radashi'

describe('onlyPath', () => {
test('should return the path without query and fragment', () => {
expect(onlyPath('/path')).toBe('/path')
})
test('should return the path without query and fragment when query is present', () => {
expect(onlyPath('/path?query=thing')).toBe('/path')
})
test('should return the path without query and fragment when fragment is present', () => {
expect(onlyPath('/path#fragment')).toBe('/path')
})
test('should return the path without query and fragment when both query and fragment are present', () => {
expect(onlyPath('/path?query=thing#fragment')).toBe('/path')
})
test('should return undefined if input is undefined', () => {
expect(onlyPath(undefined)).toBe(undefined)
})
test('should return null if input is null', () => {
expect(onlyPath(null)).toBe(null)
})
})

0 comments on commit a865d01

Please sign in to comment.