-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathe.mts
570 lines (540 loc) · 14.8 KB
/
pathe.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/**
* @file Interfaces - Pathe
* @module pathe/interfaces/Pathe
*/
import type {
ErrInvalidArgValue,
ErrInvalidFileUrlHost,
ErrInvalidFileUrlPath,
ErrInvalidUrlScheme
} from '@flex-development/errnode'
import type {
DeviceRoot,
Dot,
EmptyString,
Ext,
FileUrlToPathOptions,
PathToFileUrlOptions,
PosixPlatformPath,
ResolveWithOptions,
Sep,
ToPathOptions
} from '@flex-development/pathe'
/**
* Utilities for working with directory paths, file paths, and file extensions.
*
* @see {@linkcode PosixPlatformPath}
*
* @extends {PosixPlatformPath}
*/
interface Pathe extends PosixPlatformPath {
/**
* Append a file extension to `input`.
*
* Does nothing if a file extension is not provided, or the
* {@linkcode extname} of `input` is already `ext`.
*
* @this {void}
*
* @param {string} input
* The path or URL string to handle
* @param {string | null | undefined} ext
* The file extension to add
* @return {string}
* `input` unmodified or with new extension
*/
addExt(this: void, input: string, ext: string | null | undefined): string
/**
* Append a file extension to `url`.
*
* Does nothing if a file extension is not provided, or the
* {@linkcode extname} of `url` is already `url`.
*
* @this {void}
*
* @param {URL} url
* The {@linkcode URL} to handle
* @param {string | null | undefined} ext
* The file extension to add
* @return {URL}
* `url` unmodified or with new extension
*/
addExt(this: void, url: URL, ext: string | null | undefined): URL
/**
* Append a file extension to `input`.
*
* Does nothing if a file extension is not provided, or the
* {@linkcode extname} of `input` is already `ext`.
*
* @this {void}
*
* @param {URL | string} input
* The {@linkcode URL}, URL string, or path to handle
* @param {string | null | undefined} ext
* The file extension to add
* @return {URL | string}
* `input` unmodified or with new extension
*/
addExt(
this: void,
input: URL | string,
ext: string | null | undefined
): URL | string
/**
* Change the file extension of `input`.
*
* Does nothing if the file extension of `input` is already `ext`.
*
* @this {void}
*
* @param {string} input
* The path or URL string to handle
* @param {string | null | undefined} [ext]
* The file extension to add
* @return {string}
* `input` unmodified or with changed file extension
*/
changeExt(this: void, input: string, ext?: string | null | undefined): string
/**
* Change the file extension of `url`.
*
* Does nothing if the file extension of `url` is already `ext`.
*
* @this {void}
*
* @param {URL} url
* The {@linkcode URL} to handle
* @param {string | null | undefined} [ext]
* The file extension to add
* @return {URL}
* `url` unmodified or with changed file extension
*/
changeExt(this: void, url: URL, ext?: string | null | undefined): URL
/**
* Change the file extension of `input`.
*
* Does nothing if the file extension of `input` is already `ext`.
*
* @this {void}
*
* @param {URL | string} input
* The {@linkcode URL}, URL string, or path to handle
* @param {string | null | undefined} [ext]
* The file extension to add
* @return {URL | string}
* `input` unmodified or with changed file extension
*/
changeExt(
this: void,
input: URL | string,
ext?: string | null | undefined
): URL | string
/**
* Get the path to the current working directory.
*
* @this {void}
*
* @return {string}
* Absolute path to current working directory
*/
cwd(this: void): string
/**
* Dot character (`'.'`).
*
* @see {@linkcode Dot}
*
* @readonly
*/
readonly dot: Dot
/**
* Get a value for `input` based on its file extension.
*
* This algorithm picks the value with the longest matching file extension,
* so if `map` has the keys `'.mts'` and `'.d.mts'`, the value for `'.d.mts'`
* will be returned.
*
* @see {@linkcode EmptyString}
* @see {@linkcode Ext}
*
* @template {any} T
* Map value
*
* @this {void}
*
* @param {URL | string} input
* The {@linkcode URL}, URL string, or path to handle
* @param {Partial<Record<EmptyString | Ext, T>>} map
* Extension map
* @return {T | undefined}
* Value for `input` or `undefined`
*/
extToValue<T>(
this: void,
input: URL | string,
map: Partial<Record<EmptyString | Ext, T>>
): T | undefined
/**
* Get a list of file extensions for `input`.
*
* @see {@linkcode Ext}
* @see {@linkcode extname}
*
* @this {void}
*
* @param {URL | string} input
* The {@linkcode URL}, URL string, or path to handle
* @return {Ext[]}
* List of extensions
*/
extnames(this: void, input: URL | string): Ext[]
/**
* Convert a `file:` URL to a path.
*
* @see {@linkcode ErrInvalidFileUrlHost}
* @see {@linkcode ErrInvalidFileUrlPath}
* @see {@linkcode ErrInvalidUrlScheme}
* @see {@linkcode FileUrlToPathOptions}
*
* @this {void}
*
* @param {URL | string} url
* The `file:` URL object or string to convert to a path
* @param {FileUrlToPathOptions | null | undefined} [options]
* Conversion options
* @return {string}
* `url` as path
* @throws {ErrInvalidFileUrlHost}
* @throws {ErrInvalidFileUrlPath}
* @throws {ErrInvalidUrlScheme}
*/
fileURLToPath(
this: void,
url: URL | string,
options?: FileUrlToPathOptions | null | undefined
): string
/**
* Format a file extension.
*
* @see {@linkcode EmptyString}
* @see {@linkcode Ext}
*
* @this {void}
*
* @param {string | null | undefined} ext
* The file extension to format
* @return {EmptyString | Ext}
* Formatted file extension or empty string
*/
formatExt(this: void, ext: string | null | undefined): EmptyString | Ext
/**
* Check if `value` is a device root.
*
* @see {@linkcode DeviceRoot}
*
* @this {void}
*
* @param {unknown} value
* The value to check
* @return {value is DeviceRoot}
* `true` if `value` is device root, `false` otherwise
*/
isDeviceRoot(this: void, value: unknown): value is DeviceRoot
/**
* Check if `value` is a path segment separator.
*
* @see {@linkcode Sep}
*
* @this {void}
*
* @param {unknown} value
* The value to check
* @return {value is Sep}
* `true` if `value` is path segment separator, `false` otherwise
*/
isSep(this: void, value: unknown): value is Sep
/**
* Check if `value` is a {@linkcode URL} or can be parsed to a `URL`.
*
* @this {void}
*
* @param {unknown} value
* The value to check
* @return {value is URL | string}
* `true` if `value` is a `URL` or can be parsed to a `URL`
*/
isURL(this: void, value: unknown): value is URL | string
/**
* Convert a file `path` to a `file:` {@linkcode URL} string.
*
* > The following characters are percent-encoded when converting from file
* > path to a `URL`:
* >
* > - %: Only character not encoded by the `pathname` setter
* > - CR: Stripped out by the `pathname` setter (see [`whatwg/url#419`][419])
* > - LF: Stripped out by the `pathname` setter (see [`whatwg/url#419`][419])
* > - TAB: Stripped out by the `pathname` setter
*
* [419]: https://github.com/whatwg/url/issues/419
*
* @see {@linkcode ErrInvalidArgValue}
* @see {@linkcode PathToFileUrlOptions}
*
* @this {void}
*
* @param {string} path
* The path to handle
* @param {Omit<PathToFileUrlOptions, 'string'> & { string: true }} options
* Conversion options
* @param {true} options.string
* Return `file:` URL string?
* @return {string}
* `path` as `file:` URL string
* @throws {ErrInvalidArgValue}
*/
pathToFileURL(
this: void,
path: string,
options: Omit<PathToFileUrlOptions, 'string'> & { string: true }
): string
/**
* Convert a file `path` to a `file:` {@linkcode URL}.
*
* > The following characters are percent-encoded when converting from file
* > path to a `URL`:
* >
* > - %: Only character not encoded by the `pathname` setter
* > - CR: Stripped out by the `pathname` setter (see [`whatwg/url#419`][419])
* > - LF: Stripped out by the `pathname` setter (see [`whatwg/url#419`][419])
* > - TAB: Stripped out by the `pathname` setter
*
* [419]: https://github.com/whatwg/url/issues/419
*
* @see {@linkcode ErrInvalidArgValue}
* @see {@linkcode PathToFileUrlOptions}
*
* @this {void}
*
* @param {string} path
* The path to handle
* @param {PathToFileUrlOptions | null | undefined} [options]
* Conversion options
* @return {URL}
* `path` as `file:` URL
* @throws {ErrInvalidArgValue}
*/
pathToFileURL(
this: void,
path: string,
options?: PathToFileUrlOptions | null | undefined
): URL
/**
* Remove the file extension of `input`.
*
* Does nothing if `input` does not end with the provided file extension, or
* if a file extension is not provided.
*
* @this {void}
*
* @param {string} input
* The path or URL string to handle
* @param {string | null | undefined} ext
* The file extension to remove
* @return {string}
* `input` unmodified or with `ext` removed
*/
removeExt(this: void, input: string, ext: string | null | undefined): string
/**
* Remove the file extension of `url`.
*
* Does nothing if `url` does not end with the provided file extension, or
* if a file extension is not provided.
*
* @this {void}
*
* @param {URL} url
* The {@linkcode URL} to handle
* @param {string | null | undefined} ext
* The file extension to remove
* @return {URL}
* `url` unmodified or with `ext` removed
*/
removeExt(this: void, url: URL, ext: string | null | undefined): URL
/**
* Remove the file extension of `input`.
*
* Does nothing if `input` does not end with the provided file extension, or
* if a file extension is not provided.
*
* @this {void}
*
* @param {URL | string} input
* The {@linkcode URL}, URL string, or path to handle
* @param {string | null | undefined} ext
* The file extension to remove
* @return {URL | string}
* `input` unmodified or with `ext` removed
*/
removeExt(
this: void,
input: URL | string,
ext: string | null | undefined
): URL | string
/**
* Resolve a sequence of paths or path segments into an absolute path.
*
* The given sequence of paths is processed from right to left, with each
* subsequent path prepended until an absolute path is constructed.
*
* For instance, given the sequence of path segments: `/foo`, `/bar`, `baz`,
* calling `pathe.resolve('/foo', '/bar', 'baz')` would return `/bar/baz`
* because `'baz'` is not an absolute path, but `'/bar' + '/' + 'baz'` is.
*
* If, after processing all given `path` segments, an absolute path has not
* yet been generated, the current working directory is used.
*
* The resulting path is normalized and trailing separators are removed unless
* the path is resolved to the root directory.
*
* Zero-length `path` segments are ignored.
*
* If no `path` segments are passed, the absolute path of the current working
* directory is returned.
*
* @see {@linkcode ResolveWithOptions}
*
* @this {void}
*
* @param {ReadonlyArray<string> | string} paths
* Sequence of paths or path segments
* @param {ResolveWithOptions | null | undefined} [options]
* Resolution options
* @return {string}
* Absolute path
*/
resolveWith(
this: void,
paths: string | readonly string[],
options?: ResolveWithOptions | null | undefined
): string
/**
* Get the root of `input`.
*
* @this {void}
*
* @param {URL | string} input
* The {@linkcode URL}, URL string, or path to handle
* @return {string}
* Root of `input`
*/
root(this: void, input: URL | string): string
/**
* Convert `input` to a path.
*
* @see {@linkcode ToPathOptions}
*
* @this {void}
*
* @param {URL | string} input
* The {@linkcode URL}, URL string, or path to convert
* @param {ToPathOptions | null | undefined} [options]
* Conversion options
* @return {string}
* `input` as path
*/
toPath(
this: void,
input: URL | string,
options?: ToPathOptions | null | undefined
): string
/**
* Convert a list of inputs to paths.
*
* @see {@linkcode ToPathOptions}
*
* @this {void}
*
* @param {ReadonlyArray<URL | string>} list
* The list of {@linkcode URL}s, URL strings, or paths to convert
* @param {ToPathOptions | null | undefined} [options]
* Conversion options
* @return {string[]}
* List of paths
*/
toPath(
this: void,
list: readonly (URL | string)[],
options?: ToPathOptions | null | undefined
): string[]
/**
* Convert inputs to paths.
*
* @see {@linkcode ToPathOptions}
*
* @this {void}
*
* @param {ReadonlyArray<URL | string> | URL | string} value
* The {@linkcode URL}, URL string, or path to convert,
* or list of such values
* @param {ToPathOptions | null | undefined} [options]
* Conversion options
* @return {string[] | string}
* `value` as path or new list of paths
*/
toPath(
this: void,
value: readonly (URL | string)[] | URL | string,
options?: ToPathOptions | null | undefined
): string[] | string
/**
* Make separators in `input` POSIX-compliant.
*
* Supports encoded separators (e.g. `%5C`, `%5c`).
*
* @template {URL | string} Input
* The URL or path to handle
*
* @this {void}
*
* @param {Input} input
* The {@linkcode URL}, URL string, or path to handle
* @return {Input}
* `input` with POSIX-compliant separators
*/
toPosix<Input extends URL | string>(this: void, input: Input): Input
/**
* Make separators in `list` POSIX-compliant.
*
* Supports encoded separators (e.g. `%5C`, `%5c`).
*
* @template {(URL | string)[]} List
* The list to handle
*
* @this {void}
*
* @param {List} list
* The list of {@linkcode URL}s, URL strings, or paths to handle
* @return {List}
* `list` with POSIX-compliant separators
*/
toPosix<List extends (URL | string)[]>(this: void, list: List): List
/**
* Make separators in `value` POSIX-compliant.
*
* Supports encoded separators (e.g. `%5C`, `%5c`).
*
* @template {URL | string} Input
* The URL or path to handle
*
* @this {void}
*
* @param {Input | Input[]} value
* The {@linkcode URL}, URL string, or path to handle, or list of such values
* @return {Input | Input[]}
* `value` with POSIX-compliant separators
*/
toPosix<Input extends URL | string = URL | string>(
this: void,
value: Input | Input[]
): Input | Input[]
}
export type { Pathe as default }