-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to-path.mts
113 lines (103 loc) · 2.59 KB
/
to-path.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
/**
* @file toPath
* @module pathe/lib/toPath
*/
import canParseURL from '#internal/can-parse-url'
import validateURLString from '#internal/validate-url-string'
import fileURLToPath from '#lib/file-url-to-path'
import type { ToPathOptions } from '@flex-development/pathe'
export default toPath
/**
* Convert `input` to a path.
*
* @see {@linkcode ToPathOptions}
*
* @category
* utils
*
* @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
*/
function toPath(
this: void,
input: URL | string,
options?: ToPathOptions | null | undefined
): string
/**
* Convert a list of inputs to paths.
*
* @see {@linkcode ToPathOptions}
*
* @category
* utils
*
* @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
*/
function toPath(
this: void,
list: readonly (URL | string)[],
options?: ToPathOptions | null | undefined
): string[]
/**
* Convert inputs to paths.
*
* @see {@linkcode ToPathOptions}
*
* @category
* utils
*
* @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
*/
function toPath(
this: void,
value: readonly (URL | string)[] | URL | string,
options?: ToPathOptions | null | undefined
): string[] | string
/**
* @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
*/
function toPath(
this: void,
value: readonly (URL | string)[] | URL | string,
options?: ToPathOptions | null | undefined
): string[] | string {
if (Array.isArray<URL | string>(value)) {
return value.map((input, i) => {
return validateURLString(input, `value[${i}]`), toPath(input)
})
}
validateURLString(value, 'input')
if (typeof value === 'string') {
if (!canParseURL(value)) return value
value = new URL(value)
}
if (value.protocol === 'file:') return fileURLToPath(value, options)
return value.pathname
}