-
Notifications
You must be signed in to change notification settings - Fork 373
/
transformers.ts
164 lines (128 loc) · 3.67 KB
/
transformers.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
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/no-explicit-any */
// Const
const GLOBAL = (function getGlobal(): any {
if (typeof globalThis !== "undefined") return globalThis; // modern standard
if (typeof window !== "undefined") return window; // browser
if (typeof global !== "undefined") return global; // Node.js
// eslint-disable-next-line no-restricted-globals
if (typeof self !== "undefined") return self; // Web Worker
throw Error("Unable to locate global object");
})();
const ARRAY_BUFFER_SUPPORTED = typeof ArrayBuffer === "function";
const MAP_SUPPORTED = typeof Map === "function";
const SET_SUPPORTED = typeof Set === "function";
export type TransformsTypes =
| "Function"
| "HTMLElement"
| "[[NaN]]"
| "[[undefined]]"
| "[[Date]]"
| "[[RegExp]]"
| "[[Error]]"
| "[[RegExp]]"
| "[[Error]]"
| "[[ArrayBuffer]]"
| "[[TypedArray]]"
| "[[Map]]"
| "[[Set]]"
| "Arithmetic";
type Transforms = Record<TransformsTypes, (...params: any) => any>;
enum Arithmetic {
infinity,
minusInfinity,
minusZero,
}
export const transformers: Transforms = {
Arithmetic: (data) => {
if (data === Arithmetic.infinity) return Infinity;
if (data === Arithmetic.minusInfinity) return -Infinity;
if (data === Arithmetic.minusZero) return -0;
return data;
},
HTMLElement: (data: {
tagName: string;
attributes: Record<string, string>;
innerHTML: string;
}) => {
const sandbox = document.implementation.createHTMLDocument("sandbox");
try {
const element = sandbox.createElement(data.tagName);
element.innerHTML = data.innerHTML;
for (const attribute of Object.keys(data.attributes)) {
try {
element.setAttribute(attribute, data.attributes[attribute]);
} catch {
//
}
}
return element;
} catch (e) {
return data;
}
},
Function: (data) => {
const tempFun = () => {};
Object.defineProperty(tempFun, "toString", {
value: () => `function ${data.name}() {${data.body}}`,
});
return tempFun;
},
"[[NaN]]": () => {
return NaN;
},
"[[undefined]]": () => {
return void 0;
},
"[[Date]]": (val: any) => {
const date = new Date();
date.setTime(val);
return date;
},
"[[RegExp]]": (val: any) => {
return new RegExp(val.src, val.flags);
},
"[[Error]]": (val: any) => {
const Ctor = GLOBAL[val.name] || Error;
const err = new Ctor(val.message);
err.stack = val.stack;
return err;
},
"[[ArrayBuffer]]": (val: any) => {
if (ARRAY_BUFFER_SUPPORTED) {
const buffer = new ArrayBuffer(val.length);
const view = new Int8Array(buffer);
view.set(val);
return buffer;
}
return val;
},
"[[TypedArray]]": (val: any) => {
return typeof GLOBAL[val.ctorName] === "function"
? new GLOBAL[val.ctorName](val.arr)
: val.arr;
},
"[[Map]]": (val: any) => {
if (MAP_SUPPORTED) {
// NOTE: new Map(iterable) is not supported by all browsers
const map = new Map();
for (let i = 0; i < val.length; i += 2) map.set(val[i], val[i + 1]);
return map;
}
const kvArr = [];
// @ts-ignore
for (let j = 0; j < val.length; j += 2) kvArr.push([val[i], val[i + 1]]);
return kvArr;
},
"[[Set]]": (val: any) => {
if (SET_SUPPORTED) {
// NOTE: new Set(iterable) is not supported by all browsers
const set = new Set();
for (let i = 0; i < val.length; i++) set.add(val[i]);
return set;
}
return val;
},
};