@helpers4/url 2.0.0-alpha.2 → 2.0.0-alpha.21

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/llms.txt ADDED
@@ -0,0 +1,519 @@
1
+ # @helpers4/url
2
+
3
+ > Tree-shakable TypeScript utility functions for the `url` domain.
4
+ > Package: `@helpers4/url` — Version: 2.0.0-alpha.21
5
+ > License: LGPL-3.0-or-later
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install @helpers4/url
11
+ # or
12
+ pnpm add @helpers4/url
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ import { cleanPath, extractPureURI, onlyPath, ... } from '@helpers4/url';
19
+ ```
20
+
21
+ ## Functions
22
+
23
+ | Function | Description |
24
+ |---|---|
25
+ | `cleanPath` | Clean an URL by removing duplicate slashes. The protocol part of the URL is not modified. |
26
+ | `extractPureURI` | Extracts the pure URI from a URL by removing query parameters and fragments. |
27
+ | `onlyPath` | Extract only the path from an URI with optional query and fragments. For example, all these paramet |
28
+ | `parsePackageRepository` | Parse the `repository` field from `package.json` into a structured object. Supports all npm-specifi |
29
+ | `relativeURLToAbsolute` | Converts a relative URL to an absolute URL using the current document base URI. |
30
+ | `withLeadingSlash` | Adds a leading slash `/` to the given URL if it is not already present. This function is useful for |
31
+ | `withoutLeadingSlash` | Removes the leading slash `/` from the given URL if it is present. This function is useful for ensu |
32
+ | `withoutTrailingSlash` | Removes the trailing slash `/` from the given URL if it is present. This function is useful for ens |
33
+ | `withTrailingSlash` | Adds a trailing slash `/` to the given URL if it is not already present. This function is useful fo |
34
+
35
+ ---
36
+
37
+ ## API Reference
38
+
39
+ ### `cleanPath`
40
+
41
+ Clean an URL by removing duplicate slashes.
42
+ The protocol part of the URL is not modified.
43
+
44
+ ```typescript
45
+ import { cleanPath } from '@helpers4/url';
46
+
47
+ cleanPath(url: string | null | undefined): string | null | undefined
48
+ ```
49
+
50
+ **Parameters:**
51
+
52
+ - `url: string | null | undefined` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
53
+
54
+ **Returns:** `string | null | undefined` — The cleaned URL string, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
55
+
56
+ **Examples:**
57
+
58
+ *Remove duplicate slashes*
59
+
60
+ Cleans an URL by removing duplicate slashes while preserving the protocol.
61
+
62
+ ```typescript
63
+ cleanPath('/path//to///resource')
64
+ // => '/path/to/resource'
65
+ ```
66
+
67
+ *Preserve protocol*
68
+
69
+ The double slash after the protocol (http://) is not modified.
70
+
71
+ ```typescript
72
+ cleanPath('http://example.com//path')
73
+ // => 'http://example.com/path'
74
+ ```
75
+
76
+ *Handle null and undefined*
77
+
78
+ Returns null for null input and undefined for undefined input.
79
+
80
+ ```typescript
81
+ cleanPath(null) // => null
82
+ cleanPath(undefined) // => undefined
83
+ ```
84
+
85
+ ---
86
+
87
+ ### `extractPureURI`
88
+
89
+ Extracts the pure URI from a URL by removing query parameters and fragments.
90
+
91
+ ```typescript
92
+ import { extractPureURI } from '@helpers4/url';
93
+
94
+ extractPureURI(url: string): string
95
+ ```
96
+
97
+ **Parameters:**
98
+
99
+ - `url: string` — The URL string to process
100
+
101
+ **Returns:** `string` — The URI without query parameters and fragments, or the original value if undefined/null
102
+
103
+ ```typescript
104
+ import { extractPureURI } from '@helpers4/url';
105
+
106
+ extractPureURI(url: undefined): undefined
107
+ ```
108
+
109
+ **Parameters:**
110
+
111
+ - `url: undefined` — The URL string to process
112
+
113
+ **Returns:** `undefined` — The URI without query parameters and fragments, or the original value if undefined/null
114
+
115
+ ```typescript
116
+ import { extractPureURI } from '@helpers4/url';
117
+
118
+ extractPureURI(url: null): null
119
+ ```
120
+
121
+ **Parameters:**
122
+
123
+ - `url: null` — The URL string to process
124
+
125
+ **Returns:** `null` — The URI without query parameters and fragments, or the original value if undefined/null
126
+
127
+ **Examples:**
128
+
129
+ *Remove query parameters and fragments*
130
+
131
+ Strips everything after ? or # from the URL.
132
+
133
+ ```typescript
134
+ extractPureURI('https://example.com/path?query=1#section')
135
+ // => 'https://example.com/path'
136
+ ```
137
+
138
+ ---
139
+
140
+ ### `onlyPath`
141
+
142
+ Extract only the path from an URI with optional query and fragments.
143
+
144
+ For example, all these parameters will return `/path`:
145
+ - `/path`
146
+ - `/path?query=thing`
147
+ - `/path#fragment`
148
+ - `/path?query=thing#fragment`
149
+
150
+ ```typescript
151
+ import { onlyPath } from '@helpers4/url';
152
+
153
+ onlyPath(url: string): string
154
+ ```
155
+
156
+ **Parameters:**
157
+
158
+ - `url: string` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
159
+
160
+ **Returns:** `string` — The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
161
+
162
+ ```typescript
163
+ import { onlyPath } from '@helpers4/url';
164
+
165
+ onlyPath(url: null): null
166
+ ```
167
+
168
+ **Parameters:**
169
+
170
+ - `url: null` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
171
+
172
+ **Returns:** `null` — The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
173
+
174
+ ```typescript
175
+ import { onlyPath } from '@helpers4/url';
176
+
177
+ onlyPath(url: undefined): undefined
178
+ ```
179
+
180
+ **Parameters:**
181
+
182
+ - `url: undefined` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
183
+
184
+ **Returns:** `undefined` — The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
185
+
186
+ **Examples:**
187
+
188
+ *Extract the path from a URL*
189
+
190
+ Strips query parameters and fragments from a URL path.
191
+
192
+ ```typescript
193
+ onlyPath('/path?query=thing#fragment')
194
+ // => '/path'
195
+ ```
196
+
197
+ ---
198
+
199
+ ### `parsePackageRepository`
200
+
201
+ Parse the `repository` field from `package.json` into a structured object.
202
+
203
+ Supports all npm-specified formats:
204
+ - **Object form**: `{ "type": "git", "url": "...", "directory": "..." }`
205
+ - **GitHub shorthand**: `"owner/repo"` or `"github:owner/repo"`
206
+ - **Platform shorthands**: `"gitlab:owner/repo"`, `"bitbucket:owner/repo"`
207
+ - **Gist shorthand**: `"gist:<id>"`
208
+ - **URL forms**: `git+https://`, `https://`, `git://`, `git@` SSH, `git+ssh://`
209
+
210
+ Returns `undefined` for `null`, `undefined`, arrays, or values that cannot
211
+ be matched to any recognised format.
212
+
213
+ ```typescript
214
+ import { parsePackageRepository } from '@helpers4/url';
215
+
216
+ parsePackageRepository(repository: unknown): PackageRepository | undefined
217
+ ```
218
+
219
+ **Parameters:**
220
+
221
+ - `repository: unknown` — The `repository` field value from `package.json`.
222
+
223
+ **Returns:** `PackageRepository | undefined` — A parsed PackageRepository object, or `undefined` if the
224
+ input cannot be parsed.
225
+
226
+ **Examples:**
227
+
228
+ *Parse the npm canonical object form*
229
+
230
+ Parses the full object form written by npm publish.
231
+
232
+ ```typescript
233
+ parsePackageRepository({ type: 'git', url: 'git+https://github.com/helpers4/typescript.git' })
234
+ // => { type: 'git', host: 'github', slug: 'helpers4/typescript',
235
+ // owner: 'helpers4', repo: 'typescript', gistId: undefined, directory: undefined }
236
+ ```
237
+
238
+ *Parse npm shorthand forms*
239
+
240
+ npm accepts "owner/repo", "github:owner/repo", "gitlab:owner/repo" etc. as shorthand.
241
+
242
+ ```typescript
243
+ parsePackageRepository('helpers4/typescript')
244
+ // => { host: 'github', slug: 'helpers4/typescript', owner: 'helpers4', repo: 'typescript', ... }
245
+
246
+ parsePackageRepository('gitlab:myorg/myproject')
247
+ // => { host: 'gitlab', slug: 'myorg/myproject', owner: 'myorg', repo: 'myproject', ... }
248
+
249
+ parsePackageRepository('gist:11081aaa281')
250
+ // => { host: 'gist', gistId: '11081aaa281', slug: undefined, owner: undefined, ... }
251
+ ```
252
+
253
+ ---
254
+
255
+ ### `relativeURLToAbsolute`
256
+
257
+ Converts a relative URL to an absolute URL using the current document base URI.
258
+
259
+ ```typescript
260
+ import { relativeURLToAbsolute } from '@helpers4/url';
261
+
262
+ relativeURLToAbsolute(relativeUrl: string): string
263
+ ```
264
+
265
+ **Parameters:**
266
+
267
+ - `relativeUrl: string` — The relative URL to convert
268
+
269
+ **Returns:** `string` — The absolute URL
270
+
271
+ **Examples:**
272
+
273
+ *Convert a relative URL to absolute*
274
+
275
+ Prepends the base URI to a relative path, cleaning duplicate slashes.
276
+
277
+ ```typescript
278
+ relativeURLToAbsolute('/api/data')
279
+ // => 'http://localhost/api/data' (depends on document.baseURI)
280
+ ```
281
+
282
+ ---
283
+
284
+ ### `withLeadingSlash`
285
+
286
+ Adds a leading slash `/` to the given URL if it is not already present.
287
+
288
+ This function is useful for ensuring that URLs are properly formatted
289
+ with a leading slash, which is often required in web development for
290
+ consistency and to avoid issues with relative paths.
291
+
292
+ ```typescript
293
+ import { withLeadingSlash } from '@helpers4/url';
294
+
295
+ withLeadingSlash(url: string): string
296
+ ```
297
+
298
+ **Parameters:**
299
+
300
+ - `url: string` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
301
+
302
+ **Returns:** `string` — The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
303
+
304
+ ```typescript
305
+ import { withLeadingSlash } from '@helpers4/url';
306
+
307
+ withLeadingSlash(url: undefined): undefined
308
+ ```
309
+
310
+ **Parameters:**
311
+
312
+ - `url: undefined` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
313
+
314
+ **Returns:** `undefined` — The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
315
+
316
+ ```typescript
317
+ import { withLeadingSlash } from '@helpers4/url';
318
+
319
+ withLeadingSlash(url: null): null
320
+ ```
321
+
322
+ **Parameters:**
323
+
324
+ - `url: null` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
325
+
326
+ **Returns:** `null` — The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
327
+
328
+ **Examples:**
329
+
330
+ *Add a leading slash*
331
+
332
+ Ensures the URL starts with a forward slash.
333
+
334
+ ```typescript
335
+ withLeadingSlash('path/to/resource')
336
+ // => '/path/to/resource'
337
+ ```
338
+
339
+ *Already has leading slash*
340
+
341
+ Does not add a duplicate slash.
342
+
343
+ ```typescript
344
+ withLeadingSlash('/already/has/slash')
345
+ // => '/already/has/slash'
346
+ ```
347
+
348
+ ---
349
+
350
+ ### `withoutLeadingSlash`
351
+
352
+ Removes the leading slash `/` from the given URL if it is present.
353
+
354
+ This function is useful for ensuring that URLs are properly formatted
355
+ without a leading slash, which is often required in web development for
356
+ consistency and to avoid issues with relative paths.
357
+
358
+ ```typescript
359
+ import { withoutLeadingSlash } from '@helpers4/url';
360
+
361
+ withoutLeadingSlash(url: string): string
362
+ ```
363
+
364
+ **Parameters:**
365
+
366
+ - `url: string` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
367
+
368
+ **Returns:** `string` — The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
369
+
370
+ ```typescript
371
+ import { withoutLeadingSlash } from '@helpers4/url';
372
+
373
+ withoutLeadingSlash(url: undefined): undefined
374
+ ```
375
+
376
+ **Parameters:**
377
+
378
+ - `url: undefined` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
379
+
380
+ **Returns:** `undefined` — The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
381
+
382
+ ```typescript
383
+ import { withoutLeadingSlash } from '@helpers4/url';
384
+
385
+ withoutLeadingSlash(url: null): null
386
+ ```
387
+
388
+ **Parameters:**
389
+
390
+ - `url: null` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
391
+
392
+ **Returns:** `null` — The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
393
+
394
+ **Examples:**
395
+
396
+ *Remove leading slash*
397
+
398
+ Strips the leading slash from a URL path.
399
+
400
+ ```typescript
401
+ withoutLeadingSlash('/path/to/resource')
402
+ // => 'path/to/resource'
403
+ ```
404
+
405
+ ---
406
+
407
+ ### `withoutTrailingSlash`
408
+
409
+ Removes the trailing slash `/` from the given URL if it is present.
410
+
411
+ This function is useful for ensuring that URLs are properly formatted
412
+ without a trailing slash, which is often required in web development for
413
+ consistency and to avoid issues with relative paths.
414
+
415
+ ```typescript
416
+ import { withoutTrailingSlash } from '@helpers4/url';
417
+
418
+ withoutTrailingSlash(url: string): string
419
+ ```
420
+
421
+ **Parameters:**
422
+
423
+ - `url: string` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
424
+
425
+ **Returns:** `string` — The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
426
+
427
+ ```typescript
428
+ import { withoutTrailingSlash } from '@helpers4/url';
429
+
430
+ withoutTrailingSlash(url: undefined): undefined
431
+ ```
432
+
433
+ **Parameters:**
434
+
435
+ - `url: undefined` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
436
+
437
+ **Returns:** `undefined` — The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
438
+
439
+ ```typescript
440
+ import { withoutTrailingSlash } from '@helpers4/url';
441
+
442
+ withoutTrailingSlash(url: null): null
443
+ ```
444
+
445
+ **Parameters:**
446
+
447
+ - `url: null` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
448
+
449
+ **Returns:** `null` — The URL string without a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
450
+
451
+ **Examples:**
452
+
453
+ *Remove trailing slash*
454
+
455
+ Strips the trailing slash from a URL path.
456
+
457
+ ```typescript
458
+ withoutTrailingSlash('path/to/resource/')
459
+ // => 'path/to/resource'
460
+ ```
461
+
462
+ ---
463
+
464
+ ### `withTrailingSlash`
465
+
466
+ Adds a trailing slash `/` to the given URL if it is not already present.
467
+
468
+ This function is useful for ensuring that URLs are properly formatted
469
+ with a trailing slash, which is often required in web development for
470
+ consistency and to avoid issues with relative paths.
471
+
472
+ ```typescript
473
+ import { withTrailingSlash } from '@helpers4/url';
474
+
475
+ withTrailingSlash(url: string): string
476
+ ```
477
+
478
+ **Parameters:**
479
+
480
+ - `url: string` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
481
+
482
+ **Returns:** `string` — The URL string with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
483
+
484
+ ```typescript
485
+ import { withTrailingSlash } from '@helpers4/url';
486
+
487
+ withTrailingSlash(url: undefined): undefined
488
+ ```
489
+
490
+ **Parameters:**
491
+
492
+ - `url: undefined` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
493
+
494
+ **Returns:** `undefined` — The URL string with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
495
+
496
+ ```typescript
497
+ import { withTrailingSlash } from '@helpers4/url';
498
+
499
+ withTrailingSlash(url: null): null
500
+ ```
501
+
502
+ **Parameters:**
503
+
504
+ - `url: null` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
505
+
506
+ **Returns:** `null` — The URL string with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
507
+
508
+ **Examples:**
509
+
510
+ *Add a trailing slash*
511
+
512
+ Ensures the URL ends with a forward slash.
513
+
514
+ ```typescript
515
+ withTrailingSlash('path/to/resource')
516
+ // => 'path/to/resource/'
517
+ ```
518
+
519
+ ---