Skip to content

Commit

Permalink
Arrays are now DynamicArrays, Tuples are now Arrays, and... proper Tu…
Browse files Browse the repository at this point in the history
…ples exist.

* Migrating to Vitest for tests.
* Wrote tests for unwrapping and parsing
  • Loading branch information
iwoplaza committed Feb 20, 2024
1 parent b349b5b commit 78aac40
Show file tree
Hide file tree
Showing 32 changed files with 2,560 additions and 1,321 deletions.
48 changes: 35 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ The feature I am most proud of would have to be [recursive types](#recursive-typ
- [Primitives](#primitives)
- [Objects](#objects)
- [Arrays](#arrays)
- [Tuples](#tuples)
- [Dynamic Arrays](#dynamic-arrays)
- [Tuple](#tuple)
- [Optionals](#optionals)
- [Recursive types](#recursive-types)
- [Custom schema types](#custom-schema-types)
Expand Down Expand Up @@ -65,13 +66,20 @@ To properly enable type inference, **TypeScript 4.5** and up is required because
# Basic usage

```ts
import { Parsed, object, arrayOf, i32, string, bool } from 'typed-binary';
import {
Parsed,
object,
dynamicArrayOf,
i32,
string,
bool,
} from 'typed-binary';

const GameState = object({
nickname: string, // Variable-length string
stage: i32, // 32-bit integer
newGamePlus: bool, // byte-encoded boolean
collectables: arrayOf(string), // Variable-length string array
collectables: dynamicArrayOf(string), // Variable-length string array
powerUpgrades: object({
// Nested object
health: bool,
Expand Down Expand Up @@ -321,24 +329,38 @@ else {

## Arrays

First 4 bytes of encoding are the length of the array, then it's items next to one another.
The items are encoded right next to each other. No need to store length information, as that's constant (built into the schema).

```
import { i32, arrayOf } from 'typed-binary';
```ts
import { f32, arrayOf } from 'typed-binary';

const IntArray = arrayOf(i32);
const Vector2 = arrayOf(f32, 2);
const Vector3 = arrayOf(f32, 3);
const Vector4 = arrayOf(f32, 4);
```

## Tuples
## Dynamic Arrays

The items are encoded right next to each other. No need to store length information, as that's constant (built into the tuple).
First 4 bytes of encoding are the length of the array, then it's items next to one another.

```ts
import { i32, dynamicArrayOf } from 'typed-binary';

const IntArray = dynamicArrayOf(i32);
```
import { f32, tupleOf } from 'typed-binary';

const Vector2 = tupleOf(f32, 2);
const Vector3 = tupleOf(f32, 3);
const Vector4 = tupleOf(f32, 4);
## Tuple

Encodes an ordered set of schemas, one next to another.

```ts
import { f32, string, tupleOf } from 'typed-binary';

const Vec3f = tupleOf([f32, f32, f32]);
type Vec3f = Parsed<typeof Vec3f>; // [number, number, number]

const RecordEntry = tupleOf([string, Vec3f]);
type RecordEntry = Parsed<typeof RecordEntry>; // [string, [number, number, number]]
```

## Optionals
Expand Down
6 changes: 3 additions & 3 deletions examples/binaryMesh/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Parsed } from 'typed-binary';
import { object, arrayOf, tupleOf, i32 } from 'typed-binary';
import { object, dynamicArrayOf, arrayOf, i32 } from 'typed-binary';

export const Vertex = object({
x: i32,
Expand All @@ -8,11 +8,11 @@ export const Vertex = object({
});

export const Polygon = object({
vertices: tupleOf(Vertex, 3),
vertices: arrayOf(Vertex, 3),
});

export const Mesh = object({
faces: arrayOf(Polygon),
faces: dynamicArrayOf(Polygon),
});

// Helpful for the top-most level element
Expand Down
4 changes: 2 additions & 2 deletions examples/stateMachine/graph.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { i32, arrayOf, object } from 'typed-binary';
import { i32, dynamicArrayOf, object } from 'typed-binary';
import { NodeTemplate } from './node';

export const Graph = object({
entryNode: i32,
nodes: arrayOf(NodeTemplate),
nodes: dynamicArrayOf(NodeTemplate),
});
4 changes: 2 additions & 2 deletions examples/stateMachine/node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { bool, f32, i32, string, arrayOf, object } from 'typed-binary';
import { bool, f32, i32, string, dynamicArrayOf, object } from 'typed-binary';
import { ConnectionTemplate } from './connection';

export const NodeTemplate = object({
animationKey: string,
startFrame: i32,
playbackSpeed: f32,
looping: bool,
connections: arrayOf(ConnectionTemplate),
connections: dynamicArrayOf(ConnectionTemplate),
});
Loading

0 comments on commit 78aac40

Please sign in to comment.