-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
51 lines (46 loc) · 1.49 KB
/
index.test.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
import typedRegExp, { TypedRegExp } from "./index";
test("example", () => {
const urlPattern = typedRegExp(
"^(?:(?<protocol>https?:)(?:\\/\\/\\/?)(?:(?<auth>(?<user>[\\w]*)(?::(?<pass>[\\w]*))?)@)?(?<hostname>[\\d\\w\\.-]+)(?::(?<port>\\d+))?)?(?<pathname>[\\/\\\\\\w\\.()-]*)?(?:(?<search>[?](?<query>[^#]*))?(?<hash>#.*)?)*$",
"i"
);
const matches = urlPattern.exec(
"https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash"
);
if (matches) {
const { user, pass, hostname, port } = matches.groups;
expect({ user, pass, hostname, port }).toMatchInlineSnapshot(`
Object {
"hostname": "sub.example.com",
"pass": "pass",
"port": "8080",
"user": "user",
}
`);
} else {
fail("This should not be seen");
}
});
function equals<A, B>(_expect: Equals<A, B>): void {}
type Equals<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
type Names<T extends TypedRegExp<any>> = T extends TypedRegExp<infer U>
? keyof U
: never;
{
const regexp = typedRegExp("");
equals<Names<typeof regexp>, never>(true);
}
{
const regexp = typedRegExp("(?<name>.)");
equals<Names<typeof regexp>, "name">(true);
equals<Names<typeof regexp>, "name" | "other">(false);
equals<Names<typeof regexp>, "unknown">(false);
}
{
const regexp = typedRegExp("(?<outside>.(?<inside>.))");
equals<Names<typeof regexp>, "outside" | "inside">(true);
}
{
const regexp = typedRegExp("(?<first>.)(?<second>.)");
equals<Names<typeof regexp>, "first" | "second">(true);
}