-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.ts
63 lines (54 loc) · 1.48 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
60
61
62
63
import { TemplateTtesters } from "./template.ts";
// tries to traverse an object to a given path
// example: get(myObj, 'foo', 'bar')
export function get(
obj: Record<string | number | symbol, any>,
path: string,
more?: string,
): any {
if (more && obj[path]) {
return obj[path][more];
}
return obj[path];
}
// sets a nested value at a given path that is delimited by '›'
// example: set({}, 'foo›bar›baz', 123)
// output: { foo: { bar: { baz: 123}}}
export function set<T>(
target: Record<string | number | symbol, any>,
path: string,
value: T,
) {
var parts = path.split("›");
if (parts.length === 2) parts.splice(1, 0, "");
var obj = target;
var last = parts.pop()!;
parts.forEach(function (prop) {
if (!obj[prop]) obj[prop] = {};
obj = obj[prop];
});
obj[last] = value;
}
const replacements: Record<string, string> = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
"/": "/",
};
export function objectifiedTesters(): [TemplateTtesters, number] {
const decoder = new TextDecoder("utf-8");
const content = Deno.readFileSync("./testers.json");
const _testers = JSON.parse(decoder.decode(content));
const testers = {};
let count: number = 0;
Object.keys(_testers).forEach((path) => {
set(testers, path, { path: path, code: _testers[path] });
count++;
});
return [testers, count];
}
export function escape(str: string): string {
return str.replace(/[&<>"'\/]/g, (x) => replacements[x]);
}