-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathindex.ts
More file actions
171 lines (160 loc) · 4.03 KB
/
index.ts
File metadata and controls
171 lines (160 loc) · 4.03 KB
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
import { Equal, Expect } from "../test-utils";
type RemoveProtocol<T> = T extends `${infer _Protocol}://${infer Rest}`
? Rest
: T;
type RemoveFragment<T> = T extends `${infer Query}#${infer _}` ? Query : T;
type RemoveParams<T> = T extends `${infer Pathname}?${infer _Params}`
? Pathname
: T;
type ExtractProtocol<T> = T extends `${infer Protocol}://${infer _Rest}`
? `${Protocol}:`
: never;
type MaybeLiteralsAfterHostname = "/" | ":" | "?";
type ExtractHost<T extends string> = T extends string
? RemoveProtocol<T> extends `${infer Host}${MaybeLiteralsAfterHostname}${infer _}`
? Host extends `${infer Name}:${infer _Port}`
? Name
: Host extends `${infer Name}/${infer _Path}`
? Name
: Host extends `${infer Name}?${infer _Query}`
? Name
: Host
: RemoveProtocol<T>
: never;
type ExtractPathname<T extends string> =
RemoveProtocol<T> extends `${ExtractHost<T>}${infer Rest}`
? Rest extends `:${number}/${infer Pathname}`
? `/${RemoveParams<Pathname>}`
: Rest extends `:${number}`
? "/"
: RemoveParams<Rest>
: "/";
type MaybeLiteralsAfterPort = "/" | "?" | "#";
type ExtractPort<T extends string> =
// if host contains www. bail out
ExtractHost<T> extends `www.${infer _}`
? ""
: RemoveProtocol<T> extends `${infer _}:${infer Port}${MaybeLiteralsAfterPort}${infer _Rest}`
? Port extends `${number}`
? Port
: "" & never // dismiss union
: RemoveProtocol<T> extends `${infer _}:${infer Port}`
? Port
: "";
type ExtractQueryParams<T extends string> =
T extends `${infer _Pathname}?${infer Query}`
? `?${RemoveFragment<Query>}`
: "";
type ExtractHashFragment<T extends string> =
T extends `${infer _Pathname}#${infer Hash}` ? `#${Hash}` : "";
type ParseURL<T extends string> = {
hostname: ExtractHost<T>;
protocol: ExtractProtocol<T>;
pathname: ExtractPathname<T>;
search: ExtractQueryParams<T>;
port: ExtractPort<T>;
hash: ExtractHashFragment<T>;
};
type Demo0 = ParseURL<"https://tsplay.dev/WYYMdW">;
type Demo1 = ParseURL<"http://localhost.com:8000">;
type Demo2 = ParseURL<"https://anuraghazra.dev/nice/work?nice=work&nice=2">;
type Demo3 = ParseURL<"ssl://localhost.com?nice=wok">;
type Demo4 =
ParseURL<"http://www.example.com:8000/path/to/file.html?key=1&val=2&nice=d#hash">;
type Demo5 =
ParseURL<"http://example.com:22/path/to/file.html?key=1&val=2&val=3#hash">;
type Demo6 = ParseURL<"http://example.com/path/to/file.html?key=1&val=2#hash">;
type Tests = [
Expect<
Equal<
Demo0,
{
hostname: "tsplay.dev";
protocol: "https:";
pathname: "/WYYMdW";
search: "";
port: "";
hash: "";
}
>
>,
Expect<
Equal<
Demo1,
{
hostname: "localhost.com";
protocol: "http:";
pathname: "/";
search: "";
port: "8000";
hash: "";
}
>
>,
Expect<
Equal<
Demo2,
{
hostname: "anuraghazra.dev";
protocol: "https:";
pathname: "/nice/work";
search: "?nice=work&nice=2";
port: "";
hash: "";
}
>
>,
Expect<
Equal<
Demo3,
{
hostname: "localhost.com";
protocol: "ssl:";
pathname: "";
search: "?nice=wok";
port: "";
hash: "";
}
>
>,
Expect<
Equal<
Demo4,
{
hostname: "www.example.com";
protocol: "http:";
pathname: "/path/to/file.html";
search: "?key=1&val=2&nice=d";
port: "";
hash: "#hash";
}
>
>,
Expect<
Equal<
Demo5,
{
hostname: "example.com";
protocol: "http:";
pathname: "/path/to/file.html";
search: "?key=1&val=2&val=3";
port: "22";
hash: "#hash";
}
>
>,
Expect<
Equal<
Demo6,
{
hostname: "example.com";
protocol: "http:";
pathname: "/path/to/file.html";
search: "?key=1&val=2";
port: "";
hash: "#hash";
}
>
>
];
export {};