-
Notifications
You must be signed in to change notification settings - Fork 15
/
helpers_gen.go
111 lines (99 loc) · 2.68 KB
/
helpers_gen.go
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
package struct2ts
const ts_helpers = `
const maxUnixTSInSeconds = 9999999999;
function ParseDate(d: Date | number | string): Date {
if (d instanceof Date) return d;
if (typeof d === 'number') {
if (d > maxUnixTSInSeconds) return new Date(d);
return new Date(d * 1000); // go ts
}
return new Date(d);
}
function ParseNumber(v: number | string, isInt = false): number {
if (!v) return 0;
if (typeof v === 'number') return v;
return (isInt ? parseInt(v) : parseFloat(v)) || 0;
}
function FromArray<T>(Ctor: { new (v: any): T }, data?: any[] | any, def = null): T[] | null {
if (!data || !Object.keys(data).length) return def;
const d = Array.isArray(data) ? data : [data];
return d.map((v: any) => new Ctor(v));
}
function ToObject(o: any, typeOrCfg: any = {}, child = false): any {
if (o == null) return null;
if (typeof o.toObject === 'function' && child) return o.toObject();
switch (typeof o) {
case 'string':
return typeOrCfg === 'number' ? ParseNumber(o) : o;
case 'boolean':
case 'number':
return o;
}
if (o instanceof Date) {
return typeOrCfg === 'string' ? o.toISOString() : Math.floor(o.getTime() / 1000);
}
if (Array.isArray(o)) return o.map((v: any) => ToObject(v, typeOrCfg, true));
const d: any = {};
for (const k of Object.keys(o)) {
const v: any = o[k];
if (v === undefined) continue;
if (v === null) continue;
d[k] = ToObject(v, typeOrCfg[k] || {}, true);
}
return d;
}
`
const es6_helpers = `
const maxUnixTSInSeconds = 9999999999;
function ParseDate(d) {
if (d instanceof Date)
return d;
if (typeof d === 'number') {
if (d > maxUnixTSInSeconds)
return new Date(d);
return new Date(d * 1000); // go ts
}
return new Date(d);
}
function ParseNumber(v, isInt = false) {
if (!v)
return 0;
if (typeof v === 'number')
return v;
return (isInt ? parseInt(v) : parseFloat(v)) || 0;
}
function FromArray(Ctor, data, def = null) {
if (!data || !Object.keys(data).length)
return def;
const d = Array.isArray(data) ? data : [data];
return d.map((v) => new Ctor(v));
}
function ToObject(o, typeOrCfg = {}, child = false) {
if (o == null)
return null;
if (typeof o.toObject === 'function' && child)
return o.toObject();
switch (typeof o) {
case 'string':
return typeOrCfg === 'number' ? ParseNumber(o) : o;
case 'boolean':
case 'number':
return o;
}
if (o instanceof Date) {
return typeOrCfg === 'string' ? o.toISOString() : Math.floor(o.getTime() / 1000);
}
if (Array.isArray(o))
return o.map((v) => ToObject(v, typeOrCfg, true));
const d = {};
for (const k of Object.keys(o)) {
const v = o[k];
if (v === undefined)
continue;
if (v === null)
continue;
d[k] = ToObject(v, typeOrCfg[k] || {}, true);
}
return d;
}
`