-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathutils.ts
258 lines (222 loc) Β· 6.38 KB
/
utils.ts
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
import { $URL } from "./url";
import { parseURL, stringifyParsedURL } from "./parse";
import { QueryObject, parseQuery, stringifyQuery, ParsedQuery } from "./query";
import { decode } from "./encoding";
export function isRelative(inputString: string) {
return ["./", "../"].some((string_) => inputString.startsWith(string_));
}
const PROTOCOL_STRICT_REGEX = /^[\s\w\0+.-]{2,}:([/\\]{1,2})/;
const PROTOCOL_REGEX = /^[\s\w\0+.-]{2,}:([/\\]{2})?/;
const PROTOCOL_RELATIVE_REGEX = /^([/\\]\s*){2,}[^/\\]/;
export interface HasProtocolOptions {
acceptRelative?: boolean;
strict?: boolean;
}
export function hasProtocol(
inputString: string,
opts?: HasProtocolOptions
): boolean;
/**
* @deprecated
* Same as { hasProtocol(inputString, { acceptRelative: true })
*/
export function hasProtocol(
inputString: string,
acceptRelative: boolean
): boolean;
export function hasProtocol(
inputString: string,
opts: boolean | HasProtocolOptions = {}
): boolean {
if (typeof opts === "boolean") {
opts = { acceptRelative: opts };
}
if (opts.strict) {
return PROTOCOL_STRICT_REGEX.test(inputString);
}
return (
PROTOCOL_REGEX.test(inputString) ||
(opts.acceptRelative ? PROTOCOL_RELATIVE_REGEX.test(inputString) : false)
);
}
const PROTOCOL_SCRIPT_RE = /^[\s\0]*(blob|data|javascript|vbscript):$/i;
export function isScriptProtocol(protocol?: string) {
return !!protocol && PROTOCOL_SCRIPT_RE.test(protocol);
}
const TRAILING_SLASH_RE = /\/$|\/\?|\/#/;
export function hasTrailingSlash(
input = "",
respectQueryAndFragment?: boolean
): boolean {
if (!respectQueryAndFragment) {
return input.endsWith("/");
}
return TRAILING_SLASH_RE.test(input);
}
export function withoutTrailingSlash(
input = "",
respectQueryAndFragment?: boolean
): string {
if (!respectQueryAndFragment) {
return (hasTrailingSlash(input) ? input.slice(0, -1) : input) || "/";
}
if (!hasTrailingSlash(input, true)) {
return input || "/";
}
let path = input;
let fragment = "";
const fragmentIndex = input.indexOf("#");
if (fragmentIndex >= 0) {
path = input.slice(0, fragmentIndex);
fragment = input.slice(fragmentIndex);
}
const [s0, ...s] = path.split("?");
return (
(s0.slice(0, -1) || "/") +
(s.length > 0 ? `?${s.join("?")}` : "") +
fragment
);
}
export function withTrailingSlash(
input = "",
respectQueryAndFragment?: boolean
): string {
if (!respectQueryAndFragment) {
return input.endsWith("/") ? input : input + "/";
}
if (hasTrailingSlash(input, true)) {
return input || "/";
}
let path = input;
let fragment = "";
const fragmentIndex = input.indexOf("#");
if (fragmentIndex >= 0) {
path = input.slice(0, fragmentIndex);
fragment = input.slice(fragmentIndex);
if (!path) {
return fragment;
}
}
const [s0, ...s] = path.split("?");
return s0 + "/" + (s.length > 0 ? `?${s.join("?")}` : "") + fragment;
}
export function hasLeadingSlash(input = ""): boolean {
return input.startsWith("/");
}
export function withoutLeadingSlash(input = ""): string {
return (hasLeadingSlash(input) ? input.slice(1) : input) || "/";
}
export function withLeadingSlash(input = ""): string {
return hasLeadingSlash(input) ? input : "/" + input;
}
export function cleanDoubleSlashes(input = ""): string {
return input
.split("://")
.map((string_) => string_.replace(/\/{2,}/g, "/"))
.join("://");
}
export function withBase(input: string, base: string) {
if (isEmptyURL(base) || hasProtocol(input)) {
return input;
}
const _base = withoutTrailingSlash(base);
if (input.startsWith(_base)) {
return input;
}
return joinURL(_base, input);
}
export function withoutBase(input: string, base: string) {
if (isEmptyURL(base)) {
return input;
}
const _base = withoutTrailingSlash(base);
if (!input.startsWith(_base)) {
return input;
}
const trimmed = input.slice(_base.length);
return trimmed[0] === "/" ? trimmed : "/" + trimmed;
}
export function withQuery(input: string, query: QueryObject): string {
const parsed = parseURL(input);
const mergedQuery = { ...parseQuery(parsed.search), ...query };
parsed.search = stringifyQuery(mergedQuery);
return stringifyParsedURL(parsed);
}
export function getQuery<T extends ParsedQuery = ParsedQuery>(
input: string
): T {
return parseQuery<T>(parseURL(input).search);
}
export function isEmptyURL(url: string) {
return !url || url === "/";
}
export function isNonEmptyURL(url: string) {
return url && url !== "/";
}
const JOIN_LEADING_SLASH_RE = /^\.?\//;
export function joinURL(base: string, ...input: string[]): string {
let url = base || "";
for (const segment of input.filter((url) => isNonEmptyURL(url))) {
if (url) {
// TODO: Handle .. when joining
const _segment = segment.replace(JOIN_LEADING_SLASH_RE, "");
url = withTrailingSlash(url) + _segment;
} else {
url = segment;
}
}
return url;
}
export function withHttp(input: string): string {
return withProtocol(input, "http://");
}
export function withHttps(input: string): string {
return withProtocol(input, "https://");
}
export function withoutProtocol(input: string): string {
return withProtocol(input, "");
}
export function withProtocol(input: string, protocol: string): string {
const match = input.match(PROTOCOL_REGEX);
if (!match) {
return protocol + input;
}
return protocol + input.slice(match[0].length);
}
// $URL based utils
export function createURL(input: string): $URL {
return new $URL(input);
}
export function normalizeURL(input: string): string {
return createURL(input).toString();
}
export function resolveURL(base: string, ...input: string[]): string {
const url = createURL(base);
for (const index of input.filter((url) => isNonEmptyURL(url))) {
url.append(createURL(index));
}
return url.toString();
}
export function isSamePath(p1: string, p2: string) {
return decode(withoutTrailingSlash(p1)) === decode(withoutTrailingSlash(p2));
}
interface CompareURLOptions {
trailingSlash?: boolean;
leadingSlash?: boolean;
encoding?: boolean;
}
export function isEqual(a: string, b: string, options: CompareURLOptions = {}) {
if (!options.trailingSlash) {
a = withTrailingSlash(a);
b = withTrailingSlash(b);
}
if (!options.leadingSlash) {
a = withLeadingSlash(a);
b = withLeadingSlash(b);
}
if (!options.encoding) {
a = decode(a);
b = decode(b);
}
return a === b;
}