-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
to-posix.mts
118 lines (106 loc) · 2.56 KB
/
to-posix.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
/**
* @file toPosix
* @module pathe/lib/toPosix
*/
import validateURLString from '#internal/validate-url-string'
import sep from '#lib/sep'
export default toPosix
/**
* Make separators in `input` POSIX-compliant.
*
* Supports encoded separators (e.g. `%5C`, `%5c`).
*
* @category
* utils
*
* @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
*/
function toPosix<Input extends URL | string>(this: void, input: Input): Input
/**
* Make separators in `list` POSIX-compliant.
*
* Supports encoded separators (e.g. `%5C`, `%5c`).
*
* @category
* utils
*
* @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
*/
function toPosix<List extends (URL | string)[]>(this: void, list: List): List
/**
* Make separators in `value` POSIX-compliant.
*
* Supports encoded separators (e.g. `%5C`, `%5c`).
*
* @category
* utils
*
* @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
*/
function toPosix<Input extends URL | string>(
this: void,
value: Input | Input[]
): Input | Input[]
/**
* @this {void}
*
* @param {(URL | string)[] | URL | string} value
* The {@linkcode URL}, URL string, or path to handle, or list of such values
* @return {(URL | string)[] | URL | string}
* `value` with POSIX-compliant separators
*/
function toPosix(
this: void,
value: (URL | string)[] | URL | string
): (URL | string)[] | URL | string {
if (Array.isArray<URL | string>(value)) {
/**
* Current index in {@linkcode value}.
*
* @var {number} i
*/
let i: number = -1
while (++i < value.length) {
/**
* The URL, URL string, or path to handle.
*
* @const {URL | string} input
*/
const input: URL | string = value[i]!
validateURLString(input, `value[${i}]`)
value[i] = toPosix(input)
}
return value
}
validateURLString(value, 'value')
if (typeof value === 'string') {
return value.replace(/\\/g, sep)
.replace(/(?:%5C)/g, '%2F')
.replace(/(?:%5c)/g, '%2f')
}
return value.href = toPosix(value.href), value
}