@helpers4/url 1.0.1 → 2.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -9,9 +9,64 @@ A set of helpers for working with URLs.
9
9
 
10
10
  ## Documentation
11
11
 
12
- [https://helpers4.github.io/url](https://helpers4.github.io/url)
12
+ [https://helpers4.dev/typescript/categories/url/](https://helpers4.dev/typescript/categories/url/)
13
+
14
+ <!-- AUTOMATIC-METHODS -->
15
+ - cleanPath
16
+ - extractPureURI
17
+ - onlyPath
18
+ - relativeURLToAbsolute
19
+ - withLeadingSlash
20
+ - withTrailingSlash
21
+ - withoutLeadingSlash
22
+ - withoutTrailingSlash
23
+ <!-- /AUTOMATIC-METHODS -->
13
24
 
14
25
  ## See
15
26
 
16
- - [@helpers4/string](https://www.npmjs.com/package/@helpers4/string)
17
- - [@helpers4/url](https://www.npmjs.com/package/@helpers4/url)
27
+ <!-- AUTOMATIC-SIBLINGS -->
28
+ - [array](../array)
29
+ - [date](../date)
30
+ - [function](../function)
31
+ - [math](../math)
32
+ - [number](../number)
33
+ - [object](../object)
34
+ - [observable](../observable)
35
+ - [promise](../promise)
36
+ - [string](../string)
37
+ - [type](../type)
38
+ - [version](../version)
39
+ <!-- /AUTOMATIC-SIBLINGS -->
40
+
41
+ ## All Available Packages
42
+
43
+ <!-- AUTOMATIC-CATEGORIES-TABLE -->
44
+ | Name | Package | Source Code | Description |
45
+ |------|---------|-------------|-------------|
46
+ | array | [@helpers4/array](https://www.npmjs.com/package/@helpers4/array) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/array) | Array manipulation utilities |
47
+ | date | [@helpers4/date](https://www.npmjs.com/package/@helpers4/date) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/date) | date utilities |
48
+ | function | [@helpers4/function](https://www.npmjs.com/package/@helpers4/function) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/function) | Function utilities and type guards |
49
+ | math | [@helpers4/math](https://www.npmjs.com/package/@helpers4/math) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/math) | math utilities |
50
+ | number | [@helpers4/number](https://www.npmjs.com/package/@helpers4/number) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/number) | number utilities |
51
+ | object | [@helpers4/object](https://www.npmjs.com/package/@helpers4/object) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/object) | Object manipulation utilities |
52
+ | observable | [@helpers4/observable](https://www.npmjs.com/package/@helpers4/observable) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/observable) | Observable utilities and combinators |
53
+ | promise | [@helpers4/promise](https://www.npmjs.com/package/@helpers4/promise) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/promise) | Promise utilities and error handling |
54
+ | string | [@helpers4/string](https://www.npmjs.com/package/@helpers4/string) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/string) | String manipulation and formatting utilities |
55
+ | type | [@helpers4/type](https://www.npmjs.com/package/@helpers4/type) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/type) | type utilities |
56
+ | url | [@helpers4/url](https://www.npmjs.com/package/@helpers4/url) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/url) | URL parsing and manipulation utilities |
57
+ | version | [@helpers4/version](https://www.npmjs.com/package/@helpers4/version) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/version) | Version string manipulation utilities |
58
+ <!-- /AUTOMATIC-CATEGORIES-TABLE -->
59
+
60
+ ## Contributors
61
+
62
+ <table>
63
+ <tr>
64
+ <td align="center" style="word-wrap: break-word; width: 150.0; height: 150.0">
65
+ <a href=https://github.com/baxyz>
66
+ <img src=https://avatars.githubusercontent.com/u/7852177?v=4 width="100;" alt=baxyz/>
67
+ <br />
68
+ <sub style="font-size:14px"><b>baxyz</b></sub>
69
+ </a>
70
+ </td>
71
+ </tr>
72
+ </table>
package/lib/index.d.ts ADDED
@@ -0,0 +1,437 @@
1
+ /**
2
+ * This file is part of helpers4.
3
+ * Copyright (C) 2025 baxyz
4
+ * SPDX-License-Identifier: LGPL-3.0-or-later
5
+ */
6
+ /**
7
+ * Clean an URL by removing duplicate slashes.
8
+ * The protocol part of the URL is not modified.
9
+ *
10
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
11
+ * @returns The cleaned URL string, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * cleanPath('/path//to///resource') // => '/path/to/resource'
16
+ * cleanPath('http://example.com//path//to///resource') // => 'http://example.com/path/to/resource'
17
+ * cleanPath(undefined) // => undefined
18
+ * cleanPath(null) // => null
19
+ * ```
20
+ * @since 1.0.0
21
+ */
22
+ declare function cleanPath(url: string | undefined | null): string | undefined | null;
23
+
24
+ /**
25
+ * This file is part of helpers4.
26
+ * Copyright (C) 2025 baxyz
27
+ * SPDX-License-Identifier: LGPL-3.0-or-later
28
+ */
29
+ /**
30
+ * Extracts the pure URI from a URL by removing query parameters and fragments.
31
+ *
32
+ * @param url - The URL string to process
33
+ * @returns The URI without query parameters and fragments, or the original value if undefined/null
34
+ * @since 1.9.0
35
+ */
36
+ declare function extractPureURI(url: string): string;
37
+ declare function extractPureURI(url: undefined): undefined;
38
+ declare function extractPureURI(url: null): null;
39
+
40
+ /**
41
+ * This file is part of helpers4.
42
+ * Copyright (C) 2025 baxyz
43
+ * SPDX-License-Identifier: LGPL-3.0-or-later
44
+ */
45
+ /**
46
+ * Extract only the path from an URI with optional query and fragments.
47
+ *
48
+ * For example, all these parameters will return `/path`:
49
+ * - `/path`
50
+ * - `/path?query=thing`
51
+ * - `/path#fragment`
52
+ * - `/path?query=thing#fragment`
53
+ *
54
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
55
+ * @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * onlyPath('/path') // => '/path'
60
+ * onlyPath('/path?query=thing') // => '/path'
61
+ * onlyPath('/path#fragment') // => '/path'
62
+ * onlyPath('/path?query=thing#fragment') // => '/path'
63
+ * onlyPath(undefined) // => undefined
64
+ * onlyPath(null) // => null
65
+ * ```
66
+ * @since 1.0.0
67
+ */
68
+ declare function onlyPath(url: string): string;
69
+ /**
70
+ * Extract only the path from an URI with optional query and fragments.
71
+ *
72
+ * For example, all these parameters will return `/path`:
73
+ * - `/path`
74
+ * - `/path?query=thing`
75
+ * - `/path#fragment`
76
+ * - `/path?query=thing#fragment`
77
+ *
78
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
79
+ * @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * onlyPath('/path') // => '/path'
84
+ * onlyPath('/path?query=thing') // => '/path'
85
+ * onlyPath('/path#fragment') // => '/path'
86
+ * onlyPath('/path?query=thing#fragment') // => '/path'
87
+ * onlyPath(undefined) // => undefined
88
+ * onlyPath(null) // => null
89
+ * ```
90
+ * @since 1.0.0
91
+ */
92
+ declare function onlyPath(url: null): null;
93
+ /**
94
+ * Extract only the path from an URI with optional query and fragments.
95
+ *
96
+ * For example, all these parameters will return `/path`:
97
+ * - `/path`
98
+ * - `/path?query=thing`
99
+ * - `/path#fragment`
100
+ * - `/path?query=thing#fragment`
101
+ *
102
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
103
+ * @returns The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * onlyPath('/path') // => '/path'
108
+ * onlyPath('/path?query=thing') // => '/path'
109
+ * onlyPath('/path#fragment') // => '/path'
110
+ * onlyPath('/path?query=thing#fragment') // => '/path'
111
+ * onlyPath(undefined) // => undefined
112
+ * onlyPath(null) // => null
113
+ * ```
114
+ * @since 1.0.0
115
+ */
116
+ declare function onlyPath(url: undefined): undefined;
117
+
118
+ /**
119
+ * This file is part of helpers4.
120
+ * Copyright (C) 2025 baxyz
121
+ * SPDX-License-Identifier: LGPL-3.0-or-later
122
+ */
123
+ /**
124
+ * Converts a relative URL to an absolute URL using the current document base URI.
125
+ * @param relativeUrl - The relative URL to convert
126
+ * @returns The absolute URL
127
+ * @since 1.0.0
128
+ */
129
+ declare function relativeURLToAbsolute(relativeUrl: string): string;
130
+
131
+ /**
132
+ * This file is part of helpers4.
133
+ * Copyright (C) 2025 baxyz
134
+ * SPDX-License-Identifier: LGPL-3.0-or-later
135
+ */
136
+ /**
137
+ * Adds a leading slash `/` to the given URL if it is not already present.
138
+ *
139
+ * This function is useful for ensuring that URLs are properly formatted
140
+ * with a leading slash, which is often required in web development for
141
+ * consistency and to avoid issues with relative paths.
142
+ *
143
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
144
+ * @returns The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
145
+ *
146
+ * @see https://radashi.js.org/reference/url/withLeadingSlash
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * withLeadingSlash('') // => '/'
151
+ * withLeadingSlash('no/slash') // => '/no/slash'
152
+ * withLeadingSlash('/already/has/slash') // => '/already/has/slash'
153
+ * withLeadingSlash(undefined) // => undefined
154
+ * withLeadingSlash(null) // => null
155
+ * ```
156
+ * @since 1.0.0
157
+ */
158
+ declare function withLeadingSlash(url: string): string;
159
+ /**
160
+ * Adds a leading slash `/` to the given URL if it is not already present.
161
+ *
162
+ * This function is useful for ensuring that URLs are properly formatted
163
+ * with a leading slash, which is often required in web development for
164
+ * consistency and to avoid issues with relative paths.
165
+ *
166
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
167
+ * @returns The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
168
+ *
169
+ * @see https://radashi.js.org/reference/url/withLeadingSlash
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * withLeadingSlash('') // => '/'
174
+ * withLeadingSlash('no/slash') // => '/no/slash'
175
+ * withLeadingSlash('/already/has/slash') // => '/already/has/slash'
176
+ * withLeadingSlash(undefined) // => undefined
177
+ * withLeadingSlash(null) // => null
178
+ * ```
179
+ * @since 1.0.0
180
+ */
181
+ declare function withLeadingSlash(url: undefined): undefined;
182
+ /**
183
+ * Adds a leading slash `/` to the given URL if it is not already present.
184
+ *
185
+ * This function is useful for ensuring that URLs are properly formatted
186
+ * with a leading slash, which is often required in web development for
187
+ * consistency and to avoid issues with relative paths.
188
+ *
189
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
190
+ * @returns The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
191
+ *
192
+ * @see https://radashi.js.org/reference/url/withLeadingSlash
193
+ *
194
+ * @example
195
+ * ```ts
196
+ * withLeadingSlash('') // => '/'
197
+ * withLeadingSlash('no/slash') // => '/no/slash'
198
+ * withLeadingSlash('/already/has/slash') // => '/already/has/slash'
199
+ * withLeadingSlash(undefined) // => undefined
200
+ * withLeadingSlash(null) // => null
201
+ * ```
202
+ * @since 1.0.0
203
+ */
204
+ declare function withLeadingSlash(url: null): null;
205
+
206
+ /**
207
+ * This file is part of helpers4.
208
+ * Copyright (C) 2025 baxyz
209
+ * SPDX-License-Identifier: LGPL-3.0-or-later
210
+ */
211
+ /**
212
+ * Adds a trailing slash `/` to the given URL if it is not already present.
213
+ *
214
+ * This function is useful for ensuring that URLs are properly formatted
215
+ * with a trailing slash, which is often required in web development for
216
+ * consistency and to avoid issues with relative paths.
217
+ *
218
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
219
+ * @returns The URL string with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
220
+ *
221
+ * @see https://radashi.js.org/reference/url/withTrailingSlash
222
+ *
223
+ * @example
224
+ * ```ts
225
+ * withTrailingSlash('') // => '/'
226
+ * withTrailingSlash('no/slash') // => 'no/slash/'
227
+ * withTrailingSlash('already/has/slash/') // => 'already/has/slash/'
228
+ * withTrailingSlash(undefined) // => undefined
229
+ * withTrailingSlash(null) // => null
230
+ * ```
231
+ * @since 1.0.0
232
+ */
233
+ declare function withTrailingSlash(url: string): string;
234
+ /**
235
+ * Adds a trailing slash `/` to the given URL if it is not already present.
236
+ *
237
+ * This function is useful for ensuring that URLs are properly formatted
238
+ * with a trailing slash, which is often required in web development for
239
+ * consistency and to avoid issues with relative paths.
240
+ *
241
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
242
+ * @returns The URL string with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
243
+ *
244
+ * @see https://radashi.js.org/reference/url/withTrailingSlash
245
+ *
246
+ * @example
247
+ * ```ts
248
+ * withTrailingSlash('') // => '/'
249
+ * withTrailingSlash('no/slash') // => 'no/slash/'
250
+ * withTrailingSlash('already/has/slash/') // => 'already/has/slash/'
251
+ * withTrailingSlash(undefined) // => undefined
252
+ * withTrailingSlash(null) // => null
253
+ * ```
254
+ * @since 1.0.0
255
+ */
256
+ declare function withTrailingSlash(url: undefined): undefined;
257
+ /**
258
+ * Adds a trailing slash `/` to the given URL if it is not already present.
259
+ *
260
+ * This function is useful for ensuring that URLs are properly formatted
261
+ * with a trailing slash, which is often required in web development for
262
+ * consistency and to avoid issues with relative paths.
263
+ *
264
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
265
+ * @returns The URL string with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
266
+ *
267
+ * @see https://radashi.js.org/reference/url/withTrailingSlash
268
+ *
269
+ * @example
270
+ * ```ts
271
+ * withTrailingSlash('') // => '/'
272
+ * withTrailingSlash('no/slash') // => 'no/slash/'
273
+ * withTrailingSlash('already/has/slash/') // => 'already/has/slash/'
274
+ * withTrailingSlash(undefined) // => undefined
275
+ * withTrailingSlash(null) // => null
276
+ * ```
277
+ * @since 1.0.0
278
+ */
279
+ declare function withTrailingSlash(url: null): null;
280
+
281
+ /**
282
+ * This file is part of helpers4.
283
+ * Copyright (C) 2025 baxyz
284
+ * SPDX-License-Identifier: LGPL-3.0-or-later
285
+ */
286
+ /**
287
+ * Removes the leading slash `/` from the given URL if it is present.
288
+ *
289
+ * This function is useful for ensuring that URLs are properly formatted
290
+ * without a leading slash, which is often required in web development for
291
+ * consistency and to avoid issues with relative paths.
292
+ *
293
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
294
+ * @returns The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
295
+ *
296
+ * @see https://radashi.js.org/reference/url/withoutLeadingSlash
297
+ *
298
+ * @example
299
+ * ```ts
300
+ * withoutLeadingSlash('') // => ''
301
+ * withoutLeadingSlash('/') // => ''
302
+ * withoutLeadingSlash('/no/slash') // => 'no/slash'
303
+ * withoutLeadingSlash('already/has/slash') // => 'already/has/slash'
304
+ * withoutLeadingSlash(undefined) // => undefined
305
+ * withoutLeadingSlash(null) // => null
306
+ * ```
307
+ * @since 1.0.0
308
+ */
309
+ declare function withoutLeadingSlash(url: string): string;
310
+ /**
311
+ * Removes the leading slash `/` from the given URL if it is present.
312
+ *
313
+ * This function is useful for ensuring that URLs are properly formatted
314
+ * without a leading slash, which is often required in web development for
315
+ * consistency and to avoid issues with relative paths.
316
+ *
317
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
318
+ * @returns The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
319
+ *
320
+ * @see https://radashi.js.org/reference/url/withoutLeadingSlash
321
+ *
322
+ * @example
323
+ * ```ts
324
+ * withoutLeadingSlash('') // => ''
325
+ * withoutLeadingSlash('/') // => ''
326
+ * withoutLeadingSlash('/no/slash') // => 'no/slash'
327
+ * withoutLeadingSlash('already/has/slash') // => 'already/has/slash'
328
+ * withoutLeadingSlash(undefined) // => undefined
329
+ * withoutLeadingSlash(null) // => null
330
+ * ```
331
+ * @since 1.0.0
332
+ */
333
+ declare function withoutLeadingSlash(url: undefined): undefined;
334
+ /**
335
+ * Removes the leading slash `/` from the given URL if it is present.
336
+ *
337
+ * This function is useful for ensuring that URLs are properly formatted
338
+ * without a leading slash, which is often required in web development for
339
+ * consistency and to avoid issues with relative paths.
340
+ *
341
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
342
+ * @returns The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
343
+ *
344
+ * @see https://radashi.js.org/reference/url/withoutLeadingSlash
345
+ *
346
+ * @example
347
+ * ```ts
348
+ * withoutLeadingSlash('') // => ''
349
+ * withoutLeadingSlash('/') // => ''
350
+ * withoutLeadingSlash('/no/slash') // => 'no/slash'
351
+ * withoutLeadingSlash('already/has/slash') // => 'already/has/slash'
352
+ * withoutLeadingSlash(undefined) // => undefined
353
+ * withoutLeadingSlash(null) // => null
354
+ * ```
355
+ * @since 1.0.0
356
+ */
357
+ declare function withoutLeadingSlash(url: null): null;
358
+
359
+ /**
360
+ * This file is part of helpers4.
361
+ * Copyright (C) 2025 baxyz
362
+ * SPDX-License-Identifier: LGPL-3.0-or-later
363
+ */
364
+ /**
365
+ * Removes the trailing slash `/` from the given URL if it is present.
366
+ *
367
+ * This function is useful for ensuring that URLs are properly formatted
368
+ * without a trailing slash, which is often required in web development for
369
+ * consistency and to avoid issues with relative paths.
370
+ *
371
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
372
+ * @returns The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
373
+ *
374
+ * @see https://radashi.js.org/reference/url/withoutTrailingSlash
375
+ *
376
+ * @example
377
+ * ```ts
378
+ * withoutTrailingSlash('') // => ''
379
+ * withoutTrailingSlash('/') // => ''
380
+ * withoutTrailingSlash('no/slash/') // => 'no/slash'
381
+ * withoutTrailingSlash('already/has/slash') // => 'already/has/slash'
382
+ * withoutTrailingSlash(undefined) // => undefined
383
+ * withoutTrailingSlash(null) // => null
384
+ * ```
385
+ * @since 1.0.0
386
+ */
387
+ declare function withoutTrailingSlash(url: string): string;
388
+ /**
389
+ * Removes the trailing slash `/` from the given URL if it is present.
390
+ *
391
+ * This function is useful for ensuring that URLs are properly formatted
392
+ * without a trailing slash, which is often required in web development for
393
+ * consistency and to avoid issues with relative paths.
394
+ *
395
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
396
+ * @returns The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
397
+ *
398
+ * @see https://radashi.js.org/reference/url/withoutTrailingSlash
399
+ *
400
+ * @example
401
+ * ```ts
402
+ * withoutTrailingSlash('') // => ''
403
+ * withoutTrailingSlash('/') // => ''
404
+ * withoutTrailingSlash('no/slash/') // => 'no/slash'
405
+ * withoutTrailingSlash('already/has/slash') // => 'already/has/slash'
406
+ * withoutTrailingSlash(undefined) // => undefined
407
+ * withoutTrailingSlash(null) // => null
408
+ * ```
409
+ * @since 1.0.0
410
+ */
411
+ declare function withoutTrailingSlash(url: undefined): undefined;
412
+ /**
413
+ * Removes the trailing slash `/` from the given URL if it is present.
414
+ *
415
+ * This function is useful for ensuring that URLs are properly formatted
416
+ * without a trailing slash, which is often required in web development for
417
+ * consistency and to avoid issues with relative paths.
418
+ *
419
+ * @param url - The URL string to be processed. Can be `string`, `undefined`, or `null`.
420
+ * @returns The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
421
+ *
422
+ * @see https://radashi.js.org/reference/url/withoutTrailingSlash
423
+ *
424
+ * @example
425
+ * ```ts
426
+ * withoutTrailingSlash('') // => ''
427
+ * withoutTrailingSlash('/') // => ''
428
+ * withoutTrailingSlash('no/slash/') // => 'no/slash'
429
+ * withoutTrailingSlash('already/has/slash') // => 'already/has/slash'
430
+ * withoutTrailingSlash(undefined) // => undefined
431
+ * withoutTrailingSlash(null) // => null
432
+ * ```
433
+ * @since 1.0.0
434
+ */
435
+ declare function withoutTrailingSlash(url: null): null;
436
+
437
+ export { cleanPath, extractPureURI, onlyPath, relativeURLToAbsolute, withLeadingSlash, withTrailingSlash, withoutLeadingSlash, withoutTrailingSlash };