From a62120927ab48c0da98f9a8681bdf0f13197d290 Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Wed, 18 Dec 2024 14:46:39 +0100 Subject: [PATCH] refactor: Explicit return types (#43) --- packages/typed-binary/jsr.json | 10 +++++++++ packages/typed-binary/src/io/bufferIOBase.ts | 2 +- packages/typed-binary/src/io/bufferReader.ts | 22 +++++++++---------- packages/typed-binary/src/io/measurer.ts | 2 +- packages/typed-binary/src/structure/array.ts | 2 +- .../typed-binary/src/structure/baseTypes.ts | 22 +++++++++---------- packages/typed-binary/src/structure/chars.ts | 8 ++++--- .../src/structure/dynamicArray.ts | 2 +- packages/typed-binary/src/structure/keyed.ts | 2 +- packages/typed-binary/src/structure/object.ts | 11 ++++++---- .../typed-binary/src/structure/optional.ts | 4 +++- packages/typed-binary/src/structure/tuple.ts | 2 +- .../typed-binary/src/structure/typedArray.ts | 20 +++++++++-------- packages/typed-binary/src/util.ts | 2 +- 14 files changed, 65 insertions(+), 46 deletions(-) create mode 100644 packages/typed-binary/jsr.json diff --git a/packages/typed-binary/jsr.json b/packages/typed-binary/jsr.json new file mode 100644 index 0000000..067f03b --- /dev/null +++ b/packages/typed-binary/jsr.json @@ -0,0 +1,10 @@ +{ + "name": "@iwoplaza/typed-binary", + "version": "4.3.0-alpha.0", + "license": "MIT", + "exports": "./src/index.ts", + "publish": { + "include": ["./README.md", "./package.json", "./src/**/*.ts"], + "exclude": ["./src/test", "./**/*.test.ts"] + } +} diff --git a/packages/typed-binary/src/io/bufferIOBase.ts b/packages/typed-binary/src/io/bufferIOBase.ts index 2f27b6d..76b51c6 100644 --- a/packages/typed-binary/src/io/bufferIOBase.ts +++ b/packages/typed-binary/src/io/bufferIOBase.ts @@ -36,7 +36,7 @@ export class BufferIOBase { this.dataView = new DataView(unwrapped.buffer); } - get currentByteOffset() { + get currentByteOffset(): number { return this.byteOffset; } diff --git a/packages/typed-binary/src/io/bufferReader.ts b/packages/typed-binary/src/io/bufferReader.ts index 7ca8494..a3457b5 100644 --- a/packages/typed-binary/src/io/bufferReader.ts +++ b/packages/typed-binary/src/io/bufferReader.ts @@ -13,59 +13,59 @@ export class BufferReader extends BufferIOBase implements ISerialInput { return this._cachedTextDecoder; } - readBool() { + readBool(): boolean { return this.dataView.getUint8(this.byteOffset++) !== 0; } - readByte() { + readByte(): number { return this.dataView.getUint8(this.byteOffset++); } - readInt8() { + readInt8(): number { return this.dataView.getInt8(this.byteOffset++); } - readUint8() { + readUint8(): number { return this.dataView.getUint8(this.byteOffset++); } - readInt16() { + readInt16(): number { const value = this.dataView.getInt16(this.byteOffset, this.littleEndian); this.byteOffset += 2; return value; } - readUint16() { + readUint16(): number { const value = this.dataView.getUint16(this.byteOffset, this.littleEndian); this.byteOffset += 2; return value; } - readInt32() { + readInt32(): number { const value = this.dataView.getInt32(this.byteOffset, this.littleEndian); this.byteOffset += 4; return value; } - readUint32() { + readUint32(): number { const value = this.dataView.getUint32(this.byteOffset, this.littleEndian); this.byteOffset += 4; return value; } - readFloat16() { + readFloat16(): number { const value = this.dataView.getUint16(this.byteOffset, this.littleEndian); this.byteOffset += 2; return float16ToNumber(value); } - readFloat32() { + readFloat32(): number { const value = this.dataView.getFloat32(this.byteOffset, this.littleEndian); this.byteOffset += 4; return value; } - readString() { + readString(): string { // Looking for the 'NULL' byte. let strLength = 0; while (this.byteOffset + strLength < this.dataView.byteLength) { diff --git a/packages/typed-binary/src/io/measurer.ts b/packages/typed-binary/src/io/measurer.ts index 38b1396..bc45783 100644 --- a/packages/typed-binary/src/io/measurer.ts +++ b/packages/typed-binary/src/io/measurer.ts @@ -26,7 +26,7 @@ export class Measurer implements IMeasurer { return this; } - fork() { + fork(): IMeasurer { const forked = new Measurer(); forked.size = this.size; return forked; diff --git a/packages/typed-binary/src/structure/array.ts b/packages/typed-binary/src/structure/array.ts index 3f9b95a..231d32c 100644 --- a/packages/typed-binary/src/structure/array.ts +++ b/packages/typed-binary/src/structure/array.ts @@ -85,6 +85,6 @@ export class ArraySchema extends Schema< export function arrayOf( elementSchema: TSchema, length: number, -) { +): ArraySchema { return new ArraySchema(elementSchema, length); } diff --git a/packages/typed-binary/src/structure/baseTypes.ts b/packages/typed-binary/src/structure/baseTypes.ts index 2a4ef20..5667651 100644 --- a/packages/typed-binary/src/structure/baseTypes.ts +++ b/packages/typed-binary/src/structure/baseTypes.ts @@ -30,7 +30,7 @@ export class BoolSchema extends Schema { } } -export const bool = new BoolSchema(); +export const bool: BoolSchema = new BoolSchema(); //// // STRING @@ -67,7 +67,7 @@ export class StringSchema extends Schema { } } -export const string = new StringSchema(); +export const string: StringSchema = new StringSchema(); //// // i8 @@ -97,7 +97,7 @@ export class Int8Schema extends Schema { } } -export const i8 = new Int8Schema(); +export const i8: Int8Schema = new Int8Schema(); //// // u8 @@ -127,12 +127,12 @@ export class Uint8Schema extends Schema { } } -export const u8 = new Uint8Schema(); +export const u8: Uint8Schema = new Uint8Schema(); /** * Alias for `bin.u8` */ -export const byte = u8; +export const byte: Uint8Schema = u8; //// // i16 @@ -162,7 +162,7 @@ export class Int16Schema extends Schema { } } -export const i16 = new Int16Schema(); +export const i16: Int16Schema = new Int16Schema(); //// // u16 @@ -192,7 +192,7 @@ export class Uint16Schema extends Schema { } } -export const u16 = new Uint16Schema(); +export const u16: Uint16Schema = new Uint16Schema(); //// // i32 @@ -222,7 +222,7 @@ export class Int32Schema extends Schema { } } -export const i32 = new Int32Schema(); +export const i32: Int32Schema = new Int32Schema(); //// // u32 @@ -252,7 +252,7 @@ export class Uint32Schema extends Schema { } } -export const u32 = new Uint32Schema(); +export const u32: Uint32Schema = new Uint32Schema(); //// // f16 @@ -282,7 +282,7 @@ export class Float16Schema extends Schema { } } -export const f16 = new Float16Schema(); +export const f16: Float16Schema = new Float16Schema(); //// // f32 @@ -312,4 +312,4 @@ export class Float32Schema extends Schema { } } -export const f32 = new Float32Schema(); +export const f32: Float32Schema = new Float32Schema(); diff --git a/packages/typed-binary/src/structure/chars.ts b/packages/typed-binary/src/structure/chars.ts index 977bd6f..02d2083 100644 --- a/packages/typed-binary/src/structure/chars.ts +++ b/packages/typed-binary/src/structure/chars.ts @@ -3,8 +3,10 @@ import { Measurer } from '../io/measurer.ts'; import type { IMeasurer, ISerialInput, ISerialOutput } from '../io/types.ts'; import { Schema } from './types.ts'; -export class CharsSchema extends Schema { - constructor(public readonly length: number) { +export class CharsSchema< + TLength extends number = number, +> extends Schema { + constructor(public readonly length: TLength) { super(); } @@ -35,6 +37,6 @@ export class CharsSchema extends Schema { } } -export function chars(length: T) { +export function chars(length: T): CharsSchema { return new CharsSchema(length); } diff --git a/packages/typed-binary/src/structure/dynamicArray.ts b/packages/typed-binary/src/structure/dynamicArray.ts index c87c45f..83bbcd8 100644 --- a/packages/typed-binary/src/structure/dynamicArray.ts +++ b/packages/typed-binary/src/structure/dynamicArray.ts @@ -121,6 +121,6 @@ export class DynamicArraySchema extends Schema< export function dynamicArrayOf( elementSchema: TSchema, -) { +): DynamicArraySchema { return new DynamicArraySchema(elementSchema); } diff --git a/packages/typed-binary/src/structure/keyed.ts b/packages/typed-binary/src/structure/keyed.ts index 40ba82c..5443770 100644 --- a/packages/typed-binary/src/structure/keyed.ts +++ b/packages/typed-binary/src/structure/keyed.ts @@ -148,6 +148,6 @@ export class KeyedSchema< export function keyed>( key: K, inner: (ref: ISchema>) => P, -) { +): KeyedSchema { return new KeyedSchema(key, inner); } diff --git a/packages/typed-binary/src/structure/object.ts b/packages/typed-binary/src/structure/object.ts index bd4c0a3..e19de60 100644 --- a/packages/typed-binary/src/structure/object.ts +++ b/packages/typed-binary/src/structure/object.ts @@ -134,8 +134,11 @@ export class ObjectSchema> } } -export const object =

>(properties: P) => - new ObjectSchema(properties); +export function object

>( + properties: P, +): ObjectSchema

{ + return new ObjectSchema(properties); +} type UnwrapGeneric, Ext> = { [TKey in keyof Ext]: ISchema< @@ -308,7 +311,7 @@ export function generic< S extends { [Key in keyof S]: AnySchemaWithProperties; }, ->(properties: P, subTypeMap: S) { +>(properties: P, subTypeMap: S): GenericObjectSchema { return new GenericObjectSchema(SubTypeKey.STRING, properties, subTypeMap); } @@ -317,6 +320,6 @@ export function genericEnum< S extends { [Key in keyof S]: AnySchemaWithProperties; }, ->(properties: P, subTypeMap: S) { +>(properties: P, subTypeMap: S): GenericObjectSchema { return new GenericObjectSchema(SubTypeKey.ENUM, properties, subTypeMap); } diff --git a/packages/typed-binary/src/structure/optional.ts b/packages/typed-binary/src/structure/optional.ts index 2d6c315..1db6aee 100644 --- a/packages/typed-binary/src/structure/optional.ts +++ b/packages/typed-binary/src/structure/optional.ts @@ -72,6 +72,8 @@ export class OptionalSchema extends Schema< } } -export function optional(innerType: TSchema) { +export function optional( + innerType: TSchema, +): OptionalSchema { return new OptionalSchema(innerType); } diff --git a/packages/typed-binary/src/structure/tuple.ts b/packages/typed-binary/src/structure/tuple.ts index bcd8fa4..78f3139 100644 --- a/packages/typed-binary/src/structure/tuple.ts +++ b/packages/typed-binary/src/structure/tuple.ts @@ -90,6 +90,6 @@ export class TupleSchema< export function tupleOf( schemas: TSchema, -) { +): TupleSchema { return new TupleSchema(schemas); } diff --git a/packages/typed-binary/src/structure/typedArray.ts b/packages/typed-binary/src/structure/typedArray.ts index c427a4c..1db70b6 100644 --- a/packages/typed-binary/src/structure/typedArray.ts +++ b/packages/typed-binary/src/structure/typedArray.ts @@ -41,29 +41,31 @@ export class TypedArraySchema< } } -export const u8Array = (length: number) => +export const u8Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Uint8Array); -export const u8ClampedArray = (length: number) => +export const u8ClampedArray = ( + length: number, +): TypedArraySchema => new TypedArraySchema(length, Uint8ClampedArray); -export const u16Array = (length: number) => +export const u16Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Uint16Array); -export const u32Array = (length: number) => +export const u32Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Uint32Array); -export const i8Array = (length: number) => +export const i8Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Int8Array); -export const i16Array = (length: number) => +export const i16Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Int16Array); -export const i32Array = (length: number) => +export const i32Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Int32Array); -export const f32Array = (length: number) => +export const f32Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Float32Array); -export const f64Array = (length: number) => +export const f64Array = (length: number): TypedArraySchema => new TypedArraySchema(length, Float64Array); diff --git a/packages/typed-binary/src/util.ts b/packages/typed-binary/src/util.ts index 74024d9..8f2fe4f 100644 --- a/packages/typed-binary/src/util.ts +++ b/packages/typed-binary/src/util.ts @@ -10,6 +10,6 @@ function isSystemBigEndian(): boolean { return array[0] === 0; // if zero is the left-most byte, one was encoded as big endian } -export function getSystemEndianness() { +export function getSystemEndianness(): 'big' | 'little' { return isSystemBigEndian() ? 'big' : 'little'; }