-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.ts
55 lines (53 loc) · 1.23 KB
/
utilities.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
export function revampType(type: string) {
if (type == "true" || type == "null") {
type += "_";
}
type = type.split("?").slice(-1)[0];
if (type.includes(".")) {
const ns = type.split(".", 1)[0];
const t = type.split(".")[1];
return ns + "_" + t;
} else {
return type;
}
}
const typeMap: Record<string, string> = {
"int": "number",
"long": "bigint",
"bool": "boolean",
"double": "number",
"true": "true",
"string": "string",
"bytes": "Uint8Array",
"int128": "bigint",
"int256": "bigint",
"!x": "T",
};
export function convertType(
type: string,
prefix = "",
) {
if (type.startsWith("flags")) {
type = type.split("?").slice(-1)[0];
}
let isVector = false;
// toLowerCase because it is sometimes `vector` in mtproto.tl
if (type.toLowerCase().startsWith("vector")) {
isVector = true;
type = type.split("<")[1].split(">")[0];
}
const mapping = typeMap[type.toLowerCase()];
if (mapping != undefined) {
type = mapping;
} else {
type = revampType(type);
if (prefix) {
type = `${prefix}${type.endsWith("_") ? type.slice(0, -1) : type}`;
}
}
if (isVector) {
return `Array<${type}>`;
} else {
return type == "X" ? "ReturnType<T>" : type;
}
}