Skip to content

Commit c6e36bb

Browse files
authored
feat: Added missing numeric schemas. (#40)
1 parent c2e353e commit c6e36bb

File tree

8 files changed

+232
-39
lines changed

8 files changed

+232
-39
lines changed

packages/typed-binary/src/describe/index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
type AnyObjectSchema,
33
ArraySchema,
44
BoolSchema,
5-
ByteSchema,
65
CharsSchema,
76
DynamicArraySchema,
87
Float16Schema,
@@ -18,6 +17,12 @@ import {
1817
TypedArraySchema,
1918
Uint32Schema,
2019
} from '../structure';
20+
import {
21+
Int8Schema,
22+
Int16Schema,
23+
Uint8Schema,
24+
Uint16Schema,
25+
} from '../structure/baseTypes';
2126
import type {
2227
AnySchema,
2328
AnySchemaWithProperties,
@@ -31,16 +36,23 @@ export const bool = new BoolSchema();
3136

3237
export const string = new StringSchema();
3338

34-
export const byte = new ByteSchema();
39+
export const i8 = new Int8Schema();
40+
export const u8 = new Uint8Schema();
3541

36-
export const i32 = new Int32Schema();
42+
export const i16 = new Int16Schema();
43+
export const u16 = new Uint16Schema();
3744

45+
export const i32 = new Int32Schema();
3846
export const u32 = new Uint32Schema();
3947

4048
export const f16 = new Float16Schema();
41-
4249
export const f32 = new Float32Schema();
4350

51+
/**
52+
* Alias for `bin.u8`
53+
*/
54+
export const byte = u8;
55+
4456
export const chars = <T extends number>(length: T) => new CharsSchema(length);
4557

4658
export const object = <P extends Record<string, AnySchema>>(properties: P) =>

packages/typed-binary/src/io/bufferReader.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,23 @@ export class BufferReader extends BufferIOBase implements ISerialInput {
2121
return this.dataView.getUint8(this.byteOffset++);
2222
}
2323

24-
readFloat16() {
25-
const value = this.dataView.getUint16(this.byteOffset, this.littleEndian);
24+
readInt8() {
25+
return this.dataView.getInt8(this.byteOffset++);
26+
}
27+
28+
readUint8() {
29+
return this.dataView.getUint8(this.byteOffset++);
30+
}
31+
32+
readInt16() {
33+
const value = this.dataView.getInt16(this.byteOffset, this.littleEndian);
2634
this.byteOffset += 2;
27-
return float16ToNumber(value);
35+
return value;
2836
}
2937

30-
readFloat32() {
31-
const value = this.dataView.getFloat32(this.byteOffset, this.littleEndian);
32-
this.byteOffset += 4;
38+
readUint16() {
39+
const value = this.dataView.getUint16(this.byteOffset, this.littleEndian);
40+
this.byteOffset += 2;
3341
return value;
3442
}
3543

@@ -45,6 +53,18 @@ export class BufferReader extends BufferIOBase implements ISerialInput {
4553
return value;
4654
}
4755

56+
readFloat16() {
57+
const value = this.dataView.getUint16(this.byteOffset, this.littleEndian);
58+
this.byteOffset += 2;
59+
return float16ToNumber(value);
60+
}
61+
62+
readFloat32() {
63+
const value = this.dataView.getFloat32(this.byteOffset, this.littleEndian);
64+
this.byteOffset += 4;
65+
return value;
66+
}
67+
4868
readString() {
4969
// Looking for the 'NULL' byte.
5070
let strLength = 0;

packages/typed-binary/src/io/bufferWriter.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@ export class BufferWriter extends BufferIOBase implements ISerialOutput {
2121
this.dataView.setUint8(this.byteOffset++, value);
2222
}
2323

24-
writeFloat16(value: number): void {
25-
this.dataView.setUint16(
26-
this.byteOffset,
27-
numberToFloat16(value),
28-
this.littleEndian,
29-
);
24+
writeInt8(value: number) {
25+
this.dataView.setInt8(this.byteOffset++, value);
26+
}
27+
28+
writeUint8(value: number) {
29+
this.dataView.setUint8(this.byteOffset++, value);
30+
}
31+
32+
writeInt16(value: number) {
33+
this.dataView.setInt16(this.byteOffset, value, this.littleEndian);
34+
this.byteOffset += 2;
35+
}
36+
37+
writeUint16(value: number) {
38+
this.dataView.setUint16(this.byteOffset, value, this.littleEndian);
3039
this.byteOffset += 2;
3140
}
3241

@@ -40,6 +49,15 @@ export class BufferWriter extends BufferIOBase implements ISerialOutput {
4049
this.byteOffset += 4;
4150
}
4251

52+
writeFloat16(value: number): void {
53+
this.dataView.setUint16(
54+
this.byteOffset,
55+
numberToFloat16(value),
56+
this.littleEndian,
57+
);
58+
this.byteOffset += 2;
59+
}
60+
4361
writeFloat32(value: number) {
4462
this.dataView.setFloat32(this.byteOffset, value, this.littleEndian);
4563
this.byteOffset += 4;

packages/typed-binary/src/io/types.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ export type BufferView = ArrayLike<number> & ArrayBufferView;
44

55
export interface ISerialInput {
66
readBool(): boolean;
7+
/**
8+
* @deprecated Use `readUint8` instead.
9+
*/
710
readByte(): number;
11+
readInt8(): number;
12+
readUint8(): number;
13+
readInt16(): number;
14+
readUint16(): number;
815
readInt32(): number;
916
readUint32(): number;
10-
readFloat32(): number;
1117
readFloat16(): number;
18+
readFloat32(): number;
1219
readString(): string;
1320
readSlice(bufferView: BufferView, offset: number, byteLength: number): void;
1421
seekTo(offset: number): void;
@@ -19,11 +26,18 @@ export interface ISerialInput {
1926

2027
export interface ISerialOutput {
2128
writeBool(value: boolean): void;
29+
/**
30+
* @deprecated Use `writeUint8` instead.
31+
*/
2232
writeByte(value: number): void;
33+
writeInt8(value: number): void;
34+
writeUint8(value: number): void;
35+
writeInt16(value: number): void;
36+
writeUint16(value: number): void;
2337
writeInt32(value: number): void;
2438
writeUint32(value: number): void;
25-
writeFloat32(value: number): void;
2639
writeFloat16(value: number): void;
40+
writeFloat32(value: number): void;
2741
writeString(value: string): void;
2842
writeSlice(bufferView: BufferView): void;
2943
seekTo(offset: number): void;

packages/typed-binary/src/structure/baseTypes.ts

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ export class StringSchema extends Schema<string> {
7070
}
7171

7272
////
73-
// BYTE
73+
// i8
7474
////
7575

76-
export class ByteSchema extends Schema<number> {
76+
export class Int8Schema extends Schema<number> {
7777
/**
7878
* The maximum number of bytes this schema can take up.
7979
*
@@ -82,11 +82,11 @@ export class ByteSchema extends Schema<number> {
8282
readonly maxSize = 1;
8383

8484
read(input: ISerialInput): number {
85-
return input.readByte();
85+
return input.readInt8();
8686
}
8787

8888
write(output: ISerialOutput, value: number): void {
89-
output.writeByte(value);
89+
output.writeInt8(value);
9090
}
9191

9292
measure(
@@ -97,6 +97,90 @@ export class ByteSchema extends Schema<number> {
9797
}
9898
}
9999

100+
////
101+
// u8
102+
////
103+
104+
export class Uint8Schema extends Schema<number> {
105+
/**
106+
* The maximum number of bytes this schema can take up.
107+
*
108+
* Alias for `.measure(MaxValue).size`
109+
*/
110+
readonly maxSize = 1;
111+
112+
read(input: ISerialInput): number {
113+
return input.readUint8();
114+
}
115+
116+
write(output: ISerialOutput, value: number): void {
117+
output.writeUint8(value);
118+
}
119+
120+
measure(
121+
_: number | MaxValue,
122+
measurer: IMeasurer = new Measurer(),
123+
): IMeasurer {
124+
return measurer.add(1);
125+
}
126+
}
127+
128+
////
129+
// i16
130+
////
131+
132+
export class Int16Schema extends Schema<number> {
133+
/**
134+
* The maximum number of bytes this schema can take up.
135+
*
136+
* Alias for `.measure(MaxValue).size`
137+
*/
138+
readonly maxSize = 2;
139+
140+
read(input: ISerialInput): number {
141+
return input.readInt16();
142+
}
143+
144+
write(output: ISerialOutput, value: number): void {
145+
output.writeInt16(value);
146+
}
147+
148+
measure(
149+
_: number | MaxValue,
150+
measurer: IMeasurer = new Measurer(),
151+
): IMeasurer {
152+
return measurer.add(2);
153+
}
154+
}
155+
156+
////
157+
// u16
158+
////
159+
160+
export class Uint16Schema extends Schema<number> {
161+
/**
162+
* The maximum number of bytes this schema can take up.
163+
*
164+
* Alias for `.measure(MaxValue).size`
165+
*/
166+
readonly maxSize = 2;
167+
168+
read(input: ISerialInput): number {
169+
return input.readUint16();
170+
}
171+
172+
write(output: ISerialOutput, value: number): void {
173+
output.writeUint16(value);
174+
}
175+
176+
measure(
177+
_: number | MaxValue,
178+
measurer: IMeasurer = new Measurer(),
179+
): IMeasurer {
180+
return measurer.add(2);
181+
}
182+
}
183+
100184
////
101185
// i32
102186
////

packages/typed-binary/src/structure/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export {
1414
// Specific schemas
1515
BoolSchema,
1616
StringSchema,
17-
ByteSchema,
17+
Int8Schema,
18+
Uint8Schema,
19+
Int16Schema,
20+
Uint16Schema,
1821
Int32Schema,
1922
Uint32Schema,
2023
Float16Schema,

packages/typed-binary/src/test/byte.test.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)