-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/better-recursive'
- Loading branch information
Showing
31 changed files
with
1,092 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { BufferWriter, BufferReader, Schema } from 'typed-binary'; | ||
|
||
export function writeAndRead<T>(schema: Schema<T>, value: T) { | ||
const buffer = Buffer.alloc(schema.sizeOf(value)); | ||
const writer = new BufferWriter(buffer); | ||
const reader = new BufferReader(buffer); | ||
|
||
schema.write(writer, value); | ||
return schema.read(reader); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Run with `npm run example:customSchema` | ||
// | ||
|
||
import { Parsed, object } from 'typed-binary'; | ||
import { writeAndRead } from '../__util'; | ||
import { RADIANS } from './radians'; | ||
|
||
/* | ||
* ROTATION | ||
*/ | ||
|
||
type Rotation = Parsed<typeof Rotation>; | ||
const Rotation = object({ | ||
roll: RADIANS, | ||
pitch: RADIANS, | ||
yaw: RADIANS, | ||
}); | ||
|
||
console.log(writeAndRead(Rotation, { | ||
roll: -0.1, | ||
pitch: 0.12345, | ||
yaw: Math.PI + 1.12345, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ISerialInput, ISerialOutput, Schema, IRefResolver } from 'typed-binary'; | ||
|
||
/** | ||
* A schema storing radians with 2 bytes of precision. | ||
*/ | ||
class RadiansSchema extends Schema<number> { | ||
resolve(ctx: IRefResolver): void { | ||
// No inner references to resolve | ||
} | ||
|
||
read(input: ISerialInput): number { | ||
const low = input.readByte(); | ||
const high = input.readByte(); | ||
|
||
const discrete = (high << 8) | low; | ||
return discrete / 65535 * Math.PI; | ||
} | ||
|
||
write(output: ISerialOutput, value: number): void { | ||
// The value will be wrapped to be in range of [0, Math.PI) | ||
const wrapped = ((value % Math.PI) + Math.PI) % Math.PI; | ||
// Discretising the value to be ints in range of [0, 65535] | ||
const discrete = Math.min(Math.floor(wrapped / Math.PI * 65535), 65535); | ||
|
||
const low = discrete & 0xFF; | ||
const high = (discrete >> 8) & 0xFF; | ||
|
||
output.writeByte(low); | ||
output.writeByte(high); | ||
} | ||
|
||
sizeOf(_: number): number { | ||
// The size of the data serialized by this schema | ||
// doesn't depend on the actual value. It's always 2 bytes. | ||
return 2; | ||
} | ||
} | ||
|
||
export const RADIANS = new RadiansSchema(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// Run with `npm run example:genericEnumTypes` | ||
// | ||
|
||
import { BufferWriter, BufferReader, INT, STRING, BOOL, genericEnum, object } from 'typed-binary'; | ||
|
||
enum AnimalType { | ||
DOG = 0, | ||
CAT = 1, | ||
}; | ||
|
||
// Generic (enum) object schema | ||
const Animal = genericEnum({ | ||
nickname: STRING, | ||
age: INT, | ||
}, { | ||
[AnimalType.DOG]: object({ // Animal can be a dog | ||
breed: STRING, | ||
}), | ||
[AnimalType.CAT]: object({ // Animal can be a cat | ||
striped: BOOL, | ||
}), | ||
}); | ||
|
||
// A buffer to serialize into/out of | ||
const buffer = Buffer.alloc(16); | ||
const writer = new BufferWriter(buffer); | ||
const reader = new BufferReader(buffer); | ||
|
||
// Writing an Animal | ||
Animal.write(writer, { | ||
type: AnimalType.CAT, // We're specyfing which concrete type we want this object to be. | ||
|
||
// Base properties | ||
nickname: 'James', | ||
age: 5, | ||
|
||
// Concrete type specific properties | ||
striped: true, | ||
}); | ||
|
||
// Deserializing the animal | ||
const animal = Animal.read(reader); | ||
|
||
// -- Type checking works here! -- | ||
// animal.type => AnimalType | ||
if (animal.type === AnimalType.CAT) { | ||
// animal.type => AnimalType.CAT | ||
console.log("It's a cat!"); | ||
// animal.striped => bool | ||
console.log(animal.striped ? "Striped" : "Not striped"); | ||
} | ||
else { | ||
// animal.type => AnimalType.DOG | ||
console.log("It's a dog!"); | ||
// animal.breed => string | ||
console.log(`More specifically, a ${animal.breed}`); | ||
|
||
// This would result in a type error (Static typing FTW!) | ||
// console.log(`Striped: ${animal.striped}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// Run with `npm run example:genericTypes` | ||
// | ||
|
||
import { BufferWriter, BufferReader, INT, STRING, BOOL, generic, object } from 'typed-binary'; | ||
|
||
// Generic object schema | ||
const Animal = generic({ | ||
nickname: STRING, | ||
age: INT, | ||
}, { | ||
'dog': object({ // Animal can be a dog | ||
breed: STRING, | ||
}), | ||
'cat': object({ // Animal can be a cat | ||
striped: BOOL, | ||
}), | ||
}); | ||
|
||
// A buffer to serialize into/out of | ||
const buffer = Buffer.alloc(16); | ||
const writer = new BufferWriter(buffer); | ||
const reader = new BufferReader(buffer); | ||
|
||
// Writing an Animal | ||
Animal.write(writer, { | ||
type: 'cat', // We're specyfing which concrete type we want this object to be. | ||
|
||
// Base properties | ||
nickname: 'James', | ||
age: 5, | ||
|
||
// Concrete type specific properties | ||
striped: true, | ||
}); | ||
|
||
// Deserializing the animal | ||
const animal = Animal.read(reader); | ||
|
||
console.log(JSON.stringify(animal)); // { "age": 5, "striped": true ... } | ||
|
||
// -- Type checking works here! -- | ||
// animal.type => 'cat' | 'dog' | ||
if (animal.type === 'cat') { | ||
// animal.type => 'cat' | ||
console.log("It's a cat!"); | ||
// animal.striped => bool | ||
console.log(animal.striped ? "Striped" : "Not striped"); | ||
} | ||
else { | ||
// animal.type => 'dog' | ||
console.log("It's a dog!"); | ||
// animal.breed => string | ||
console.log(`More specifically, a ${animal.breed}`); | ||
|
||
// This would result in a type error (Static typing FTW!) | ||
// console.log(`Striped: ${animal.striped}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.