diff --git a/examples/customSchema/index.ts b/examples/customSchema/index.ts index 66c0b3c..fde5d79 100644 --- a/examples/customSchema/index.ts +++ b/examples/customSchema/index.ts @@ -4,7 +4,7 @@ import { Parsed, object } from 'typed-binary'; import { writeAndRead } from '../__util'; -import { RADIANS } from './radians'; +import { radians } from './radians'; /* * ROTATION @@ -12,9 +12,9 @@ import { RADIANS } from './radians'; type Rotation = Parsed; const Rotation = object({ - roll: RADIANS, - pitch: RADIANS, - yaw: RADIANS, + roll: radians, + pitch: radians, + yaw: radians, }); console.log( diff --git a/examples/customSchema/radians.ts b/examples/customSchema/radians.ts index e62b6c6..51bf119 100644 --- a/examples/customSchema/radians.ts +++ b/examples/customSchema/radians.ts @@ -2,7 +2,6 @@ import { ISerialInput, ISerialOutput, Schema, - IRefResolver, MaxValue, IMeasurer, Measurer, @@ -12,10 +11,6 @@ import { * A schema storing radians with 2 bytes of precision. */ class RadiansSchema extends Schema { - resolveReferences(ctx: IRefResolver): void { - // No inner references to resolve - } - read(input: ISerialInput): number { const low = input.readByte(); const high = input.readByte(); @@ -27,7 +22,7 @@ class RadiansSchema extends Schema { 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] + // Clipping 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; @@ -47,4 +42,4 @@ class RadiansSchema extends Schema { } } -export const RADIANS = new RadiansSchema(); +export const radians = new RadiansSchema(); diff --git a/src/index.ts b/src/index.ts index dbed47e..58a5522 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,4 +3,5 @@ export * from './describe'; export * from './io'; export * from './error'; +export { getSystemEndianness } from './util'; export type { Parsed, ParseUnwrapped } from './utilityTypes'; diff --git a/src/io/bufferIOBase.ts b/src/io/bufferIOBase.ts index 134758f..9da3e85 100644 --- a/src/io/bufferIOBase.ts +++ b/src/io/bufferIOBase.ts @@ -1,4 +1,5 @@ -import { isBigEndian } from '../util'; +import { getSystemEndianness } from '../util'; +import { Endianness } from './types'; export type BufferIOOptions = { /** @@ -8,7 +9,7 @@ export type BufferIOOptions = { /** * @default 'system' */ - endianness: 'big' | 'little' | 'system'; + endianness: Endianness | 'system'; }; export class BufferIOBase { @@ -21,6 +22,8 @@ export class BufferIOBase { protected byteOffset = 0; + public readonly endianness: Endianness; + constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) { this.byteOffset = options?.byteOffset ?? 0; @@ -38,11 +41,14 @@ export class BufferIOBase { this.helperFloatView = new Float32Array(helperBuffer); this.helperByteView = new Uint8Array(helperBuffer); - const isSystemBigEndian = isBigEndian(); - const endianness = options?.endianness ?? 'system'; - this.switchEndianness = - (endianness === 'big' && !isSystemBigEndian) || - (endianness === 'little' && isSystemBigEndian); + const systemEndianness = getSystemEndianness(); + + this.endianness = + !options || options.endianness === 'system' + ? systemEndianness + : options.endianness; + + this.switchEndianness = this.endianness !== systemEndianness; } get currentByteOffset() { diff --git a/src/io/index.ts b/src/io/index.ts index 17ad8f8..87c9bb1 100644 --- a/src/io/index.ts +++ b/src/io/index.ts @@ -1,5 +1,4 @@ -export { ISerialInput, ISerialOutput, IMeasurer } from './types'; +export { Endianness, ISerialInput, ISerialOutput, IMeasurer } from './types'; export { BufferWriter } from './bufferWriter'; export { BufferReader } from './bufferReader'; export { Measurer } from './measurer'; -export { isBigEndian } from '../util'; diff --git a/src/io/types.ts b/src/io/types.ts index 6d34d7d..dae453f 100644 --- a/src/io/types.ts +++ b/src/io/types.ts @@ -1,3 +1,5 @@ +export type Endianness = 'big' | 'little'; + export interface ISerialInput { readBool(): boolean; readByte(): number; @@ -7,6 +9,7 @@ export interface ISerialInput { readString(): string; seekTo(offset: number): void; skipBytes(bytes: number): void; + readonly endianness: Endianness; readonly currentByteOffset: number; } @@ -19,6 +22,7 @@ export interface ISerialOutput { writeString(value: string): void; seekTo(offset: number): void; skipBytes(bytes: number): void; + readonly endianness: Endianness; readonly currentByteOffset: number; } diff --git a/src/util.ts b/src/util.ts index d481641..eac7a19 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,9 +1,13 @@ /** * @returns {Boolean} true if system is big endian */ - export function isBigEndian(): boolean { - const array = new Uint8Array(4); - const view = new Uint32Array(array.buffer); +function isSystemBigEndian(): boolean { + const array = new Uint8Array(4); + const view = new Uint32Array(array.buffer); - return !((view[0] = 1) & array[0]); -} \ No newline at end of file + return !((view[0] = 1) & array[0]); +} + +export function getSystemEndianness() { + return isSystemBigEndian() ? 'big' : 'little'; +}