-
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.
* Simplified type declarations. * Updated Node versions in CI
- Loading branch information
Showing
30 changed files
with
2,839 additions
and
1,295 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.vscode | ||
|
||
dist | ||
**/node_modules |
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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import { BufferWriter, BufferReader, Schema } from 'typed-binary'; | ||
import { BufferWriter, BufferReader, AnySchema, Parsed } 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); | ||
export function writeAndRead<TSchema extends AnySchema>( | ||
schema: TSchema, | ||
value: Parsed<TSchema>, | ||
) { | ||
const buffer = Buffer.alloc(schema.measure(value).size); | ||
const writer = new BufferWriter(buffer); | ||
const reader = new BufferReader(buffer); | ||
|
||
schema.write(writer, value); | ||
return schema.read(reader); | ||
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
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 |
---|---|---|
@@ -1,39 +1,50 @@ | ||
import { ISerialInput, ISerialOutput, Schema, IRefResolver } from 'typed-binary'; | ||
import { | ||
ISerialInput, | ||
ISerialOutput, | ||
Schema, | ||
IRefResolver, | ||
MaxValue, | ||
IMeasurer, | ||
Measurer, | ||
} 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; | ||
} | ||
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); | ||
} | ||
|
||
measure( | ||
_: number | MaxValue, | ||
measurer: IMeasurer = new Measurer(), | ||
): IMeasurer { | ||
// The size of the data serialized by this schema | ||
// doesn't depend on the actual value. It's always 2 bytes. | ||
return measurer.add(2); | ||
} | ||
} | ||
|
||
export const RADIANS = new RadiansSchema(); |
Oops, something went wrong.