Skip to content

Commit e06c783

Browse files
authored
Update README.md
1 parent 0c50f08 commit e06c783

1 file changed

Lines changed: 33 additions & 13 deletions

File tree

README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
npm install tinybuf
1919
```
2020

21-
## 🕹 Example
21+
## Basic Usage
2222

2323
```ts
2424
import { defineFormat, Type } from 'tinybuf';
2525

26-
export const GameWorldData = defineFormat({
26+
export const GameWorldState = defineFormat({
2727
frameNo: Type.UInt,
2828
timeRemaining: Type.Float16,
2929
players: [
@@ -48,7 +48,7 @@ export const GameWorldData = defineFormat({
4848
Formats can be encoded directly:
4949

5050
```ts
51-
let bytes = GameWorldData.encode({
51+
let bytes = GameWorldState.encode({
5252
frameNo: 50,
5353
timeRemaining: 59.334,
5454
players: [
@@ -69,21 +69,15 @@ bytes.byteLength
6969
Or directly from objects:
7070

7171
```ts
72-
let bytes = GameWorldData.encode( obj );
72+
let bytes = GameWorldState.encode( world );
7373

7474
bytes.byteLength
7575
// 16
7676
```
7777

7878
### Decode
7979

80-
Formats can be read in a number of ways:
81-
82-
1. Simple – decode to object
83-
2. In-place – decode into an existing object
84-
3. Parser – register / decode many formats
85-
86-
#### Simple
80+
#### To Object
8781

8882
Decode as a strongly-typed object.
8983

@@ -94,12 +88,12 @@ let obj = GameWorldData.decode( bytes );
9488

9589
#### In-place
9690

97-
Use for memory effiency - extract fields directly into an existing object instance. This prevents allocating new memory.
91+
Extract fields directly into an existing object (this minimizes memory footprint).
9892

9993
```ts
10094
let obj: Decoded<typeof GameWorldData> = {} as any;
10195

102-
GameWorldData.decode( bytes, obj );
96+
GameWorldData.decodeInPlace( bytes, obj );
10397
```
10498

10599
#### Parser &ndash; Decoding registered formats
@@ -121,6 +115,32 @@ const parser = bufferParser()
121115
parser.processBuffer( bytes );
122116
```
123117

118+
## Types
119+
120+
| **Type** | **JavaScript Type** | **Bytes** | **Notes** |
121+
| :--- | :--- | :--- | :--- |
122+
| `Int` | `number` | 1-4<sup>\*</sup> | A signed integer from `-Number.MAX_SAFE_INTEGER` to `Number.MAX_SAFE_INTEGER`. |
123+
| `Int8` | `number` | 1 | A signed integer from -128 to 127. |
124+
| `Int16` | `number` | 2 | A signed integer from -32,768 to 32,767. |
125+
| `Int32` | `number` | 4 | A signed integer from -2,147,483,648 to 2,147,483,647. |
126+
| `UInt` | `number` | 1-4<sup>#</sup> | An unsigned integer from 0 to `Number.MAX_SAFE_INTEGER`. |
127+
| `UInt8` | `number` | 1 | An unsigned integerfrom 0 to 255. |
128+
| `UInt16` | `number` | 2 | An unsigned integer from 0 to 65,535. |
129+
| `UInt32` | `number` | 4 | An unsigned integer from 0 to 4,294,967,295. |
130+
| `Float64` | `number` | 8 | A 64-bit double-precision floating-point number. |
131+
| `Float32` | `number` | 4 | A 32-bit single-precision floating-point number. |
132+
| `Float16` | `number` | 2 | A 16-bit half-precision floating-point number.<br/>**Note:** Low precision. Maximum effective range ±65,504. |
133+
| `BFloat16` | `number` | 2 | A [bfloat16](https://en.wikipedia.org/wiki/Bfloat16_floating-point_format) format 16-bit half-precision floating-point number.<br/>**Note:** Lowest precision. Same effective range as `Float32`. |
134+
| `Scalar` | `number` | 1 | A signed scalar between -1.00 and 1.00 (two decimal precision). |
135+
| `UScalar` | `number` | 1 | A scalar between 0.00 and 1.00 (two decimal precision). |
136+
| `Bool` | `boolean` | 1 | A boolean value. |
137+
| `Bools` | `boolean[]` | 1<sup>¶</sup> | An array/tuple of boolean values (1 - 28) encoded as a single byte. |
138+
| `Buffer` | `Uint8Array` | 1<sup>†</sup>&nbsp;+&nbsp;n | An `ArrayBuffer` or `ArrayBufferLike`. |
139+
| `String` | `string` | 1<sup>†</sup>&nbsp;+&nbsp;n | A string (UTF-8 encoded). |
140+
| `JSON` | `any` | 1<sup>†</sup>&nbsp;+&nbsp;n | Any JSON encodable value (encoded as a string). |
141+
| `RegExp` | `RegExp` | 2<sup>†</sup>&nbsp;+&nbsp;n | JavaScript `RegExp` object. |
142+
| `Date` | `Date` | 8 | JavaScript `Date` object. |
143+
124144
## 📘 Documentation
125145
| | |
126146
| --- | :--- |

0 commit comments

Comments
 (0)