-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
59 lines (49 loc) · 1.43 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
export async function serialize (node: unknown): Promise<string> {
if (node instanceof Function) {
return node.toString()
}
if (node instanceof Date) {
return node.valueOf().toString()
}
if (node instanceof Response) {
const reader = new FileReader()
const bodyBlob = await node.blob()
reader.readAsDataURL(bodyBlob)
const bodyDataURL: string = await new Promise(res => {
reader.addEventListener("load", () => {
res(reader.result as string);
});
})
node = {
body: bodyDataURL,
status: node.status,
statusText: node.statusText,
headers: [...node.headers.entries()]
}
}
return JSON.stringify(node, replacer)
}
function replacer (_key: string, value: unknown) {
if (value instanceof Map) {
return [...value.entries()]
}
if (value instanceof Set) {
return [...value.values()]
}
return value
}
export function parseFcnString(fcnString: string) {
const args = fcnString.match(/(?<=\()(.|\n)*?(?=\))/)![0]
const bodyMatches = fcnString.match(/(?<=\{)(.|\n)*?(?=\})/)
// handle arrow fcn with no curly brackets
let body = bodyMatches
? bodyMatches[0]
: fcnString.slice(fcnString.indexOf("=>")+2)
// add return if not there
if (!body.includes("return")) {
let lastBr = body.lastIndexOf("\n")
if (lastBr < 0) lastBr = 0
body = body.slice(0,lastBr) + "return " + body.slice(lastBr)
}
return new Function(args, body)
}