@helpers4/url 2.0.0-alpha.10 → 2.0.0-alpha.14
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 +462 -0
- package/meta/api.json +1 -1
- package/package.json +3 -1
package/llms.txt
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
# @helpers4/url
|
|
2
|
+
|
|
3
|
+
> Tree-shakable TypeScript utility functions for the `url` domain.
|
|
4
|
+
> Package: `@helpers4/url` — Version: 2.0.0-alpha.14
|
|
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
|
+
| `relativeURLToAbsolute` | Converts a relative URL to an absolute URL using the current document base URI. |
|
|
29
|
+
| `withLeadingSlash` | Adds a leading slash `/` to the given URL if it is not already present. This function is useful for |
|
|
30
|
+
| `withoutLeadingSlash` | Removes the leading slash `/` from the given URL if it is present. This function is useful for ensu |
|
|
31
|
+
| `withoutTrailingSlash` | Removes the trailing slash `/` from the given URL if it is present. This function is useful for ens |
|
|
32
|
+
| `withTrailingSlash` | Adds a trailing slash `/` to the given URL if it is not already present. This function is useful fo |
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## API Reference
|
|
37
|
+
|
|
38
|
+
### `cleanPath`
|
|
39
|
+
|
|
40
|
+
Clean an URL by removing duplicate slashes.
|
|
41
|
+
The protocol part of the URL is not modified.
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { cleanPath } from '@helpers4/url';
|
|
45
|
+
|
|
46
|
+
cleanPath(url: string | null | undefined): string | null | undefined
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**Parameters:**
|
|
50
|
+
|
|
51
|
+
- `url: string | null | undefined` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
|
|
52
|
+
|
|
53
|
+
**Returns:** `string | null | undefined` — The cleaned URL string, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
54
|
+
|
|
55
|
+
**Examples:**
|
|
56
|
+
|
|
57
|
+
*Remove duplicate slashes*
|
|
58
|
+
|
|
59
|
+
Cleans an URL by removing duplicate slashes while preserving the protocol.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
cleanPath('/path//to///resource')
|
|
63
|
+
// => '/path/to/resource'
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
*Preserve protocol*
|
|
67
|
+
|
|
68
|
+
The double slash after the protocol (http://) is not modified.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
cleanPath('http://example.com//path')
|
|
72
|
+
// => 'http://example.com/path'
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
*Handle null and undefined*
|
|
76
|
+
|
|
77
|
+
Returns null for null input and undefined for undefined input.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
cleanPath(null) // => null
|
|
81
|
+
cleanPath(undefined) // => undefined
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
### `extractPureURI`
|
|
87
|
+
|
|
88
|
+
Extracts the pure URI from a URL by removing query parameters and fragments.
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { extractPureURI } from '@helpers4/url';
|
|
92
|
+
|
|
93
|
+
extractPureURI(url: string): string
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**Parameters:**
|
|
97
|
+
|
|
98
|
+
- `url: string` — The URL string to process
|
|
99
|
+
|
|
100
|
+
**Returns:** `string` — The URI without query parameters and fragments, or the original value if undefined/null
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { extractPureURI } from '@helpers4/url';
|
|
104
|
+
|
|
105
|
+
extractPureURI(url: undefined): undefined
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**Parameters:**
|
|
109
|
+
|
|
110
|
+
- `url: undefined` — The URL string to process
|
|
111
|
+
|
|
112
|
+
**Returns:** `undefined` — The URI without query parameters and fragments, or the original value if undefined/null
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { extractPureURI } from '@helpers4/url';
|
|
116
|
+
|
|
117
|
+
extractPureURI(url: null): null
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
**Parameters:**
|
|
121
|
+
|
|
122
|
+
- `url: null` — The URL string to process
|
|
123
|
+
|
|
124
|
+
**Returns:** `null` — The URI without query parameters and fragments, or the original value if undefined/null
|
|
125
|
+
|
|
126
|
+
**Examples:**
|
|
127
|
+
|
|
128
|
+
*Remove query parameters and fragments*
|
|
129
|
+
|
|
130
|
+
Strips everything after ? or # from the URL.
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
extractPureURI('https://example.com/path?query=1#section')
|
|
134
|
+
// => 'https://example.com/path'
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
### `onlyPath`
|
|
140
|
+
|
|
141
|
+
Extract only the path from an URI with optional query and fragments.
|
|
142
|
+
|
|
143
|
+
For example, all these parameters will return `/path`:
|
|
144
|
+
- `/path`
|
|
145
|
+
- `/path?query=thing`
|
|
146
|
+
- `/path#fragment`
|
|
147
|
+
- `/path?query=thing#fragment`
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { onlyPath } from '@helpers4/url';
|
|
151
|
+
|
|
152
|
+
onlyPath(url: string): string
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
**Parameters:**
|
|
156
|
+
|
|
157
|
+
- `url: string` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
|
|
158
|
+
|
|
159
|
+
**Returns:** `string` — The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import { onlyPath } from '@helpers4/url';
|
|
163
|
+
|
|
164
|
+
onlyPath(url: null): null
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**Parameters:**
|
|
168
|
+
|
|
169
|
+
- `url: null` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
|
|
170
|
+
|
|
171
|
+
**Returns:** `null` — The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
import { onlyPath } from '@helpers4/url';
|
|
175
|
+
|
|
176
|
+
onlyPath(url: undefined): undefined
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
**Parameters:**
|
|
180
|
+
|
|
181
|
+
- `url: undefined` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
|
|
182
|
+
|
|
183
|
+
**Returns:** `undefined` — The URL string without query and fragment, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
184
|
+
|
|
185
|
+
**Examples:**
|
|
186
|
+
|
|
187
|
+
*Extract the path from a URL*
|
|
188
|
+
|
|
189
|
+
Strips query parameters and fragments from a URL path.
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
onlyPath('/path?query=thing#fragment')
|
|
193
|
+
// => '/path'
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
### `relativeURLToAbsolute`
|
|
199
|
+
|
|
200
|
+
Converts a relative URL to an absolute URL using the current document base URI.
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import { relativeURLToAbsolute } from '@helpers4/url';
|
|
204
|
+
|
|
205
|
+
relativeURLToAbsolute(relativeUrl: string): string
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
**Parameters:**
|
|
209
|
+
|
|
210
|
+
- `relativeUrl: string` — The relative URL to convert
|
|
211
|
+
|
|
212
|
+
**Returns:** `string` — The absolute URL
|
|
213
|
+
|
|
214
|
+
**Examples:**
|
|
215
|
+
|
|
216
|
+
*Convert a relative URL to absolute*
|
|
217
|
+
|
|
218
|
+
Prepends the base URI to a relative path, cleaning duplicate slashes.
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
relativeURLToAbsolute('/api/data')
|
|
222
|
+
// => 'http://localhost/api/data' (depends on document.baseURI)
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
### `withLeadingSlash`
|
|
228
|
+
|
|
229
|
+
Adds a leading slash `/` to the given URL if it is not already present.
|
|
230
|
+
|
|
231
|
+
This function is useful for ensuring that URLs are properly formatted
|
|
232
|
+
with a leading slash, which is often required in web development for
|
|
233
|
+
consistency and to avoid issues with relative paths.
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
import { withLeadingSlash } from '@helpers4/url';
|
|
237
|
+
|
|
238
|
+
withLeadingSlash(url: string): string
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
**Parameters:**
|
|
242
|
+
|
|
243
|
+
- `url: string` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
|
|
244
|
+
|
|
245
|
+
**Returns:** `string` — The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
import { withLeadingSlash } from '@helpers4/url';
|
|
249
|
+
|
|
250
|
+
withLeadingSlash(url: undefined): undefined
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
**Parameters:**
|
|
254
|
+
|
|
255
|
+
- `url: undefined` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
|
|
256
|
+
|
|
257
|
+
**Returns:** `undefined` — The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
import { withLeadingSlash } from '@helpers4/url';
|
|
261
|
+
|
|
262
|
+
withLeadingSlash(url: null): null
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
**Parameters:**
|
|
266
|
+
|
|
267
|
+
- `url: null` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
|
|
268
|
+
|
|
269
|
+
**Returns:** `null` — The URL string with a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
270
|
+
|
|
271
|
+
**Examples:**
|
|
272
|
+
|
|
273
|
+
*Add a leading slash*
|
|
274
|
+
|
|
275
|
+
Ensures the URL starts with a forward slash.
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
withLeadingSlash('path/to/resource')
|
|
279
|
+
// => '/path/to/resource'
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
*Already has leading slash*
|
|
283
|
+
|
|
284
|
+
Does not add a duplicate slash.
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
withLeadingSlash('/already/has/slash')
|
|
288
|
+
// => '/already/has/slash'
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
### `withoutLeadingSlash`
|
|
294
|
+
|
|
295
|
+
Removes the leading slash `/` from the given URL if it is present.
|
|
296
|
+
|
|
297
|
+
This function is useful for ensuring that URLs are properly formatted
|
|
298
|
+
without a leading slash, which is often required in web development for
|
|
299
|
+
consistency and to avoid issues with relative paths.
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
import { withoutLeadingSlash } from '@helpers4/url';
|
|
303
|
+
|
|
304
|
+
withoutLeadingSlash(url: string): string
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
**Parameters:**
|
|
308
|
+
|
|
309
|
+
- `url: string` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
|
|
310
|
+
|
|
311
|
+
**Returns:** `string` — The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
import { withoutLeadingSlash } from '@helpers4/url';
|
|
315
|
+
|
|
316
|
+
withoutLeadingSlash(url: undefined): undefined
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
**Parameters:**
|
|
320
|
+
|
|
321
|
+
- `url: undefined` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
|
|
322
|
+
|
|
323
|
+
**Returns:** `undefined` — The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
import { withoutLeadingSlash } from '@helpers4/url';
|
|
327
|
+
|
|
328
|
+
withoutLeadingSlash(url: null): null
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
**Parameters:**
|
|
332
|
+
|
|
333
|
+
- `url: null` — The URL string to be processed. Can be `string`, `undefined`, or `null`.
|
|
334
|
+
|
|
335
|
+
**Returns:** `null` — The URL string without a leading slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
336
|
+
|
|
337
|
+
**Examples:**
|
|
338
|
+
|
|
339
|
+
*Remove leading slash*
|
|
340
|
+
|
|
341
|
+
Strips the leading slash from a URL path.
|
|
342
|
+
|
|
343
|
+
```typescript
|
|
344
|
+
withoutLeadingSlash('/path/to/resource')
|
|
345
|
+
// => 'path/to/resource'
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
---
|
|
349
|
+
|
|
350
|
+
### `withoutTrailingSlash`
|
|
351
|
+
|
|
352
|
+
Removes the trailing 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 trailing slash, which is often required in web development for
|
|
356
|
+
consistency and to avoid issues with relative paths.
|
|
357
|
+
|
|
358
|
+
```typescript
|
|
359
|
+
import { withoutTrailingSlash } from '@helpers4/url';
|
|
360
|
+
|
|
361
|
+
withoutTrailingSlash(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 trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
369
|
+
|
|
370
|
+
```typescript
|
|
371
|
+
import { withoutTrailingSlash } from '@helpers4/url';
|
|
372
|
+
|
|
373
|
+
withoutTrailingSlash(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 trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
381
|
+
|
|
382
|
+
```typescript
|
|
383
|
+
import { withoutTrailingSlash } from '@helpers4/url';
|
|
384
|
+
|
|
385
|
+
withoutTrailingSlash(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 trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
393
|
+
|
|
394
|
+
**Examples:**
|
|
395
|
+
|
|
396
|
+
*Remove trailing slash*
|
|
397
|
+
|
|
398
|
+
Strips the trailing slash from a URL path.
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
withoutTrailingSlash('path/to/resource/')
|
|
402
|
+
// => 'path/to/resource'
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
---
|
|
406
|
+
|
|
407
|
+
### `withTrailingSlash`
|
|
408
|
+
|
|
409
|
+
Adds a trailing slash `/` to the given URL if it is not already present.
|
|
410
|
+
|
|
411
|
+
This function is useful for ensuring that URLs are properly formatted
|
|
412
|
+
with 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 { withTrailingSlash } from '@helpers4/url';
|
|
417
|
+
|
|
418
|
+
withTrailingSlash(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 with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
426
|
+
|
|
427
|
+
```typescript
|
|
428
|
+
import { withTrailingSlash } from '@helpers4/url';
|
|
429
|
+
|
|
430
|
+
withTrailingSlash(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 with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
438
|
+
|
|
439
|
+
```typescript
|
|
440
|
+
import { withTrailingSlash } from '@helpers4/url';
|
|
441
|
+
|
|
442
|
+
withTrailingSlash(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 with a trailing slash, or `undefined` if the input is `undefined`, or `null` if the input is `null`.
|
|
450
|
+
|
|
451
|
+
**Examples:**
|
|
452
|
+
|
|
453
|
+
*Add a trailing slash*
|
|
454
|
+
|
|
455
|
+
Ensures the URL ends with a forward slash.
|
|
456
|
+
|
|
457
|
+
```typescript
|
|
458
|
+
withTrailingSlash('path/to/resource')
|
|
459
|
+
// => 'path/to/resource/'
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
---
|
package/meta/api.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helpers4/url",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.14",
|
|
4
4
|
"description": "A set of helpers in TS/JS, compatible with tree-shaking, for url.",
|
|
5
5
|
"author": "baxyz <baxy@etik.com>",
|
|
6
6
|
"license": "LGPL-3.0",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"./meta/api.json": "./meta/api.json",
|
|
22
22
|
"./meta/examples.json": "./meta/examples.json",
|
|
23
23
|
"./meta/licenses.json": "./meta/licenses.json",
|
|
24
|
+
"./llms.txt": "./llms.txt",
|
|
24
25
|
"./package.json": "./package.json"
|
|
25
26
|
},
|
|
26
27
|
"keywords": [
|
|
@@ -40,6 +41,7 @@
|
|
|
40
41
|
"lib/index.d.ts",
|
|
41
42
|
"lib/index.js.map",
|
|
42
43
|
"meta/",
|
|
44
|
+
"llms.txt",
|
|
43
45
|
"LICENSE.md",
|
|
44
46
|
"package.json",
|
|
45
47
|
"README.md"
|