-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnumeric-view.ts
170 lines (144 loc) · 4.88 KB
/
numeric-view.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
165
166
167
168
169
170
import type { PrimitiveView, ViewConstructor } from "./view-types.ts";
export class Uint8View extends DataView implements PrimitiveView<number> {
static viewLength = 1;
static decode(view: DataView, start = 0): number {
return DataView.prototype.getUint8.call(view, start);
}
static encode(value: number, view: DataView, start = 0): number {
DataView.prototype.setUint8.call(view, start, value);
return this.viewLength;
}
static from(value: number) {
const typeView = new this(new ArrayBuffer(this.viewLength));
this.encode(value, typeView, 0);
return typeView;
}
static getLength(): number {
return this.viewLength;
}
get(): number {
return (this.constructor as typeof Uint8View).decode(this);
}
set(value: number): this {
(this.constructor as typeof Uint8View).encode(value, this);
return this;
}
toJSON(): number {
return (this.constructor as typeof Uint8View).decode(this);
}
static initialize(): ViewConstructor<number, PrimitiveView<number>> {
return this;
}
}
export class Int8View extends Uint8View {
static viewLength = 1;
static decode(view: DataView, start = 0): number {
return DataView.prototype.getInt8.call(view, start);
}
static encode(value: number, view: DataView, start = 0): number {
DataView.prototype.setInt8.call(view, start, value);
return this.viewLength;
}
}
export class Int16View extends Uint8View {
static viewLength = 2;
static decode(view: DataView, start = 0): number {
return DataView.prototype.getInt16.call(view, start, true);
}
static encode(value: number, view: DataView, start = 0): number {
DataView.prototype.setInt16.call(view, start, value, true);
return this.viewLength;
}
}
export class Uint16View extends Uint8View {
static viewLength = 2;
static decode(view: DataView, start = 0): number {
return DataView.prototype.getUint16.call(view, start, true);
}
static encode(value: number, view: DataView, start = 0): number {
DataView.prototype.setUint16.call(view, start, value, true);
return this.viewLength;
}
}
export class Int32View extends Uint8View {
static viewLength = 4;
static decode(view: DataView, start = 0): number {
return DataView.prototype.getInt32.call(view, start, true);
}
static encode(value: number, view: DataView, start = 0): number {
DataView.prototype.setInt32.call(view, start, value, true);
return this.viewLength;
}
}
export class Uint32View extends Uint8View {
static viewLength = 4;
static decode(view: DataView, start = 0): number {
return DataView.prototype.getUint32.call(view, start, true);
}
static encode(value: number, view: DataView, start = 0): number {
DataView.prototype.setUint32.call(view, start, value, true);
return this.viewLength;
}
}
export class Float32View extends Uint8View {
static viewLength = 4;
static decode(view: DataView, start = 0): number {
return DataView.prototype.getFloat32.call(view, start, true);
}
static encode(value: number, view: DataView, start = 0): number {
DataView.prototype.setFloat32.call(view, start, value, true);
return this.viewLength;
}
}
export class Float64View extends Uint8View {
static viewLength = 8;
static decode(view: DataView, start = 0): number {
return DataView.prototype.getFloat64.call(view, start, true);
}
static encode(value: number, view: DataView, start = 0): number {
DataView.prototype.setFloat64.call(view, start, value, true);
return this.viewLength;
}
}
export class BigInt64View extends DataView implements PrimitiveView<bigint> {
static viewLength = 8;
static decode(view: DataView, start = 0): bigint {
return DataView.prototype.getBigInt64.call(view, start, true);
}
static encode(value: bigint, view: DataView, start = 0): number {
DataView.prototype.setBigInt64.call(view, start, value, true);
return this.viewLength;
}
static from(value: bigint) {
const typeView = new this(new ArrayBuffer(this.viewLength));
this.encode(value, typeView, 0);
return typeView;
}
static getLength(): number {
return this.viewLength;
}
get(): bigint {
return (this.constructor as typeof BigInt64View).decode(this);
}
set(value: bigint): this {
(this.constructor as typeof BigInt64View).encode(value, this);
return this;
}
toJSON(): bigint {
// todo fix bigint is not serializable in JSON
return (this.constructor as typeof BigInt64View).decode(this);
}
static initialize(): ViewConstructor<bigint, PrimitiveView<bigint>> {
return this;
}
}
export class BigUint64View extends BigInt64View {
static viewLength = 8;
static decode(view: DataView, start = 0): bigint {
return DataView.prototype.getBigUint64.call(view, start, true);
}
static encode(value: bigint, view: DataView, start = 0): number {
DataView.prototype.setBigUint64.call(view, start, value, true);
return this.viewLength;
}
}