-
Notifications
You must be signed in to change notification settings - Fork 21
/
boolean-view.ts
41 lines (32 loc) · 1.05 KB
/
boolean-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
import type { PrimitiveView, ViewConstructor } from "./view-types.ts";
export class BooleanView extends DataView implements PrimitiveView<boolean> {
static viewLength = 1;
static decode(view: DataView, start = 0): boolean {
return !!DataView.prototype.getUint8.call(view, start);
}
static encode(value: boolean, view: DataView, start = 0): number {
DataView.prototype.setUint8.call(view, start, +value);
return this.viewLength;
}
static from(value: boolean) {
const typeView = new this(new ArrayBuffer(this.viewLength));
this.encode(value, typeView, 0);
return typeView;
}
static getLength(): number {
return this.viewLength;
}
get(): boolean {
return (this.constructor as typeof BooleanView).decode(this);
}
set(value: boolean): this {
(this.constructor as typeof BooleanView).encode(value, this);
return this;
}
toJSON(): boolean {
return (this.constructor as typeof BooleanView).decode(this);
}
static initialize(): ViewConstructor<boolean, PrimitiveView<boolean>> {
return this;
}
}