Skip to content

Commit b1c6996

Browse files
committed
feat: add url/withoutLeadingSlash
1 parent 01cee83 commit b1c6996

File tree

5 files changed

+166
-0
lines changed

5 files changed

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

docs/url/withoutLeadingSlash.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: withoutLeadingSlash
3+
description: Removes the leading slash
4+
---
5+
6+
### Usage
7+
8+
Removes the leading 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 leading slash, which is often required in web development for
12+
consistency and to avoid issues with relative paths.
13+
14+
```ts
15+
import { withoutLeadingSlash } from 'radashi'
16+
17+
withoutLeadingSlash('') // => ''
18+
withoutLeadingSlash('/no/slash') // => 'no/slash'
19+
withoutLeadingSlash('already/has/slash') // => 'already/has/slash'
20+
withoutLeadingSlash(undefined) // => undefined
21+
withoutLeadingSlash(null) // => null
22+
```

src/mod.ts

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

145145
export * from './url/withLeadingSlash.ts'
146+
export * from './url/withoutLeadingSlash.ts'
146147
export * from './url/withTrailingSlash.ts'
147148

148149
export * from './types.ts'

src/url/withoutLeadingSlash.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* Removes the leading 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 leading 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 leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
10+
*
11+
* @see https://radashi.js.org/reference/url/withoutLeadingSlash
12+
*
13+
* @example
14+
* ```ts
15+
* withoutLeadingSlash('') // => ''
16+
* withoutLeadingSlash('/') // => ''
17+
* withoutLeadingSlash('/no/slash') // => 'no/slash'
18+
* withoutLeadingSlash('already/has/slash') // => 'already/has/slash'
19+
* withoutLeadingSlash(undefined) // => undefined
20+
* withoutLeadingSlash(null) // => null
21+
* ```
22+
*/
23+
export function withoutLeadingSlash(url: string): string
24+
25+
/**
26+
* Removes the leading 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 leading 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 leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
34+
*
35+
* @see https://radashi.js.org/reference/url/withoutLeadingSlash
36+
*
37+
* @example
38+
* ```ts
39+
* withoutLeadingSlash('') // => ''
40+
* withoutLeadingSlash('/') // => ''
41+
* withoutLeadingSlash('/no/slash') // => 'no/slash'
42+
* withoutLeadingSlash('already/has/slash') // => 'already/has/slash'
43+
* withoutLeadingSlash(undefined) // => undefined
44+
* withoutLeadingSlash(null) // => null
45+
* ```
46+
*/
47+
export function withoutLeadingSlash(url: undefined): undefined
48+
49+
/**
50+
* Removes the leading 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 leading 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 leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
58+
*
59+
* @see https://radashi.js.org/reference/url/withoutLeadingSlash
60+
*
61+
* @example
62+
* ```ts
63+
* withoutLeadingSlash('') // => ''
64+
* withoutLeadingSlash('/') // => ''
65+
* withoutLeadingSlash('/no/slash') // => 'no/slash'
66+
* withoutLeadingSlash('already/has/slash') // => 'already/has/slash'
67+
* withoutLeadingSlash(undefined) // => undefined
68+
* withoutLeadingSlash(null) // => null
69+
* ```
70+
*/
71+
export function withoutLeadingSlash(url: null): null
72+
73+
/**
74+
* Removes the leading 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 leading 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 leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
82+
*
83+
* @see https://radashi.js.org/reference/url/withoutLeadingSlash
84+
*
85+
* @example
86+
* ```ts
87+
* withoutLeadingSlash('') // => ''
88+
* withoutLeadingSlash('/') // => ''
89+
* withoutLeadingSlash('/no/slash') // => 'no/slash'
90+
* withoutLeadingSlash('already/has/slash') // => 'already/has/slash'
91+
* withoutLeadingSlash(undefined) // => undefined
92+
* withoutLeadingSlash(null) // => null
93+
* ```
94+
*/
95+
export function withoutLeadingSlash(
96+
url: string | undefined | null,
97+
): string | undefined | null {
98+
if (url === undefined || url === null) {
99+
return url
100+
}
101+
return url[0] === '/' ? url.slice(1) : url
102+
}

tests/url/withoutLeadingSlash.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { withoutLeadingSlash } from 'radashi'
2+
3+
describe('withoutLeadingSlash', () => {
4+
test('should return undefined if input is undefined', () => {
5+
expect(withoutLeadingSlash(undefined)).toBe(undefined)
6+
})
7+
test('should return null if input is null', () => {
8+
expect(withoutLeadingSlash(null)).toBe(null)
9+
})
10+
test('should remove leading slash if present', () => {
11+
expect(withoutLeadingSlash('/foo')).toBe('foo')
12+
})
13+
test('should do nothing if input is an empty string', () => {
14+
expect(withoutLeadingSlash('')).toBe('')
15+
})
16+
test('should do nothing if input does not have a leading slash', () => {
17+
expect(withoutLeadingSlash('text-without-slash')).toBe('text-without-slash')
18+
})
19+
test('should remove first slash if input is a string of slashes', () => {
20+
expect(withoutLeadingSlash('/////////')).toBe('////////')
21+
})
22+
test('should return an empty string if input is a single slash', () => {
23+
expect(withoutLeadingSlash('/')).toBe('')
24+
})
25+
})

0 commit comments

Comments
 (0)