Skip to content

Commit feb10fe

Browse files
committed
feat: add url/withoutTrailingSlash
1 parent b1c6996 commit feb10fe

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { withoutTrailingSlash } from 'radashi'
2+
3+
describe('withoutTrailingSlash', () => {
4+
bench('with no input', () => {
5+
withoutTrailingSlash(undefined)
6+
})
7+
bench('with empty string', () => {
8+
withoutTrailingSlash('')
9+
})
10+
bench('with trailing slash', () => {
11+
withoutTrailingSlash('some/path/')
12+
})
13+
bench('without trailing slash', () => {
14+
withoutTrailingSlash('some/path')
15+
})
16+
})

docs/url/withoutTrailingSlash.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: withoutTrailingSlash
3+
description: Removes the trailing slash
4+
---
5+
6+
### Usage
7+
8+
Removes the trailing slash `/` from the given URL if it is present.
9+
10+
This function is useful for ensuring that URLs are properly formatted
11+
without a trailing slash, which is often required in web development for
12+
consistency and to avoid issues with relative paths.
13+
14+
```ts
15+
import { withoutTrailingSlash } from 'radashi'
16+
17+
withoutTrailingSlash('') // => ''
18+
withoutTrailingSlash('no/slash/') // => 'no/slash'
19+
withoutTrailingSlash('already/has/slash') // => 'already/has/slash'
20+
withoutTrailingSlash(undefined) // => undefined
21+
withoutTrailingSlash(null) // => null
22+
```

src/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export * from './typed/isWeakSet.ts'
144144

145145
export * from './url/withLeadingSlash.ts'
146146
export * from './url/withoutLeadingSlash.ts'
147+
export * from './url/withoutTrailingSlash.ts'
147148
export * from './url/withTrailingSlash.ts'
148149

149150
export * from './types.ts'

src/url/withoutTrailingSlash.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* Removes the trailing slash `/` from the given URL if it is present.
3+
*
4+
* This function is useful for ensuring that URLs are properly formatted
5+
* without a trailing slash, which is often required in web development for
6+
* consistency and to avoid issues with relative paths.
7+
*
8+
* @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
9+
* @returns The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
10+
*
11+
* @see https://radashi.js.org/reference/url/withoutTrailingSlash
12+
*
13+
* @example
14+
* ```ts
15+
* withoutTrailingSlash('') // => ''
16+
* withoutTrailingSlash('/') // => ''
17+
* withoutTrailingSlash('no/slash/') // => 'no/slash'
18+
* withoutTrailingSlash('already/has/slash') // => 'already/has/slash'
19+
* withoutTrailingSlash(undefined) // => undefined
20+
* withoutTrailingSlash(null) // => null
21+
* ```
22+
*/
23+
export function withoutTrailingSlash(url: string): string
24+
25+
/**
26+
* Removes the trailing slash `/` from the given URL if it is present.
27+
*
28+
* This function is useful for ensuring that URLs are properly formatted
29+
* without a trailing slash, which is often required in web development for
30+
* consistency and to avoid issues with relative paths.
31+
*
32+
* @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
33+
* @returns The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
34+
*
35+
* @see https://radashi.js.org/reference/url/withoutTrailingSlash
36+
*
37+
* @example
38+
* ```ts
39+
* withoutTrailingSlash('') // => ''
40+
* withoutTrailingSlash('/') // => ''
41+
* withoutTrailingSlash('no/slash/') // => 'no/slash'
42+
* withoutTrailingSlash('already/has/slash') // => 'already/has/slash'
43+
* withoutTrailingSlash(undefined) // => undefined
44+
* withoutTrailingSlash(null) // => null
45+
* ```
46+
*/
47+
export function withoutTrailingSlash(url: undefined): undefined
48+
49+
/**
50+
* Removes the trailing slash `/` from the given URL if it is present.
51+
*
52+
* This function is useful for ensuring that URLs are properly formatted
53+
* without a trailing slash, which is often required in web development for
54+
* consistency and to avoid issues with relative paths.
55+
*
56+
* @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
57+
* @returns The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
58+
*
59+
* @see https://radashi.js.org/reference/url/withoutTrailingSlash
60+
*
61+
* @example
62+
* ```ts
63+
* withoutTrailingSlash('') // => ''
64+
* withoutTrailingSlash('/') // => ''
65+
* withoutTrailingSlash('no/slash/') // => 'no/slash'
66+
* withoutTrailingSlash('already/has/slash') // => 'already/has/slash'
67+
* withoutTrailingSlash(undefined) // => undefined
68+
* withoutTrailingSlash(null) // => null
69+
* ```
70+
*/
71+
export function withoutTrailingSlash(url: null): null
72+
73+
/**
74+
* Removes the trailing slash `/` from the given URL if it is present.
75+
*
76+
* This function is useful for ensuring that URLs are properly formatted
77+
* without a trailing slash, which is often required in web development for
78+
* consistency and to avoid issues with relative paths.
79+
*
80+
* @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
81+
* @returns The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
82+
*
83+
* @see https://radashi.js.org/reference/url/withoutTrailingSlash
84+
*
85+
* @example
86+
* ```ts
87+
* withoutTrailingSlash('') // => ''
88+
* withoutTrailingSlash('/') // => ''
89+
* withoutTrailingSlash('no/slash/') // => 'no/slash'
90+
* withoutTrailingSlash('already/has/slash') // => 'already/has/slash'
91+
* withoutTrailingSlash(undefined) // => undefined
92+
* withoutTrailingSlash(null) // => null
93+
* ```
94+
*/
95+
export function withoutTrailingSlash(
96+
url: string | undefined | null,
97+
): string | undefined | null {
98+
if (url === undefined || url === null) {
99+
return url
100+
}
101+
return url[url.length - 1] === '/' ? url.slice(0, -1) : url
102+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { withoutTrailingSlash } from 'radashi'
2+
3+
describe('withoutTrailingSlash', () => {
4+
test('should return undefined if input is undefined', () => {
5+
expect(withoutTrailingSlash(undefined)).toBe(undefined)
6+
})
7+
test('should return null if input is null', () => {
8+
expect(withoutTrailingSlash(null)).toBe(null)
9+
})
10+
test('should remove trailing slash if present', () => {
11+
expect(withoutTrailingSlash('foo/')).toBe('foo')
12+
})
13+
test('should do nothing if input is an empty string', () => {
14+
expect(withoutTrailingSlash('')).toBe('')
15+
})
16+
test('should do nothing if input does not have a trailing slash', () => {
17+
expect(withoutTrailingSlash('text-without-slash')).toBe(
18+
'text-without-slash',
19+
)
20+
})
21+
test('should remove last slash if input is a string of slashes', () => {
22+
expect(withoutTrailingSlash('/////////')).toBe('////////')
23+
})
24+
test('should return an empty string if input is a single slash', () => {
25+
expect(withoutTrailingSlash('/')).toBe('')
26+
})
27+
})

0 commit comments

Comments
 (0)