Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Explicit return types #43

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/typed-binary/jsr.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}
2 changes: 1 addition & 1 deletion packages/typed-binary/src/io/bufferIOBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class BufferIOBase {
this.dataView = new DataView(unwrapped.buffer);
}

get currentByteOffset() {
get currentByteOffset(): number {
return this.byteOffset;
}

Expand Down
22 changes: 11 additions & 11 deletions packages/typed-binary/src/io/bufferReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/typed-binary/src/io/measurer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Measurer implements IMeasurer {
return this;
}

fork() {
fork(): IMeasurer {
const forked = new Measurer();
forked.size = this.size;
return forked;
Expand Down
2 changes: 1 addition & 1 deletion packages/typed-binary/src/structure/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ export class ArraySchema<TElement extends AnySchema> extends Schema<
export function arrayOf<TSchema extends AnySchema>(
elementSchema: TSchema,
length: number,
) {
): ArraySchema<TSchema> {
return new ArraySchema(elementSchema, length);
}
22 changes: 11 additions & 11 deletions packages/typed-binary/src/structure/baseTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class BoolSchema extends Schema<boolean> {
}
}

export const bool = new BoolSchema();
export const bool: BoolSchema = new BoolSchema();

////
// STRING
Expand Down Expand Up @@ -67,7 +67,7 @@ export class StringSchema extends Schema<string> {
}
}

export const string = new StringSchema();
export const string: StringSchema = new StringSchema();

////
// i8
Expand Down Expand Up @@ -97,7 +97,7 @@ export class Int8Schema extends Schema<number> {
}
}

export const i8 = new Int8Schema();
export const i8: Int8Schema = new Int8Schema();

////
// u8
Expand Down Expand Up @@ -127,12 +127,12 @@ export class Uint8Schema extends Schema<number> {
}
}

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
Expand Down Expand Up @@ -162,7 +162,7 @@ export class Int16Schema extends Schema<number> {
}
}

export const i16 = new Int16Schema();
export const i16: Int16Schema = new Int16Schema();

////
// u16
Expand Down Expand Up @@ -192,7 +192,7 @@ export class Uint16Schema extends Schema<number> {
}
}

export const u16 = new Uint16Schema();
export const u16: Uint16Schema = new Uint16Schema();

////
// i32
Expand Down Expand Up @@ -222,7 +222,7 @@ export class Int32Schema extends Schema<number> {
}
}

export const i32 = new Int32Schema();
export const i32: Int32Schema = new Int32Schema();

////
// u32
Expand Down Expand Up @@ -252,7 +252,7 @@ export class Uint32Schema extends Schema<number> {
}
}

export const u32 = new Uint32Schema();
export const u32: Uint32Schema = new Uint32Schema();

////
// f16
Expand Down Expand Up @@ -282,7 +282,7 @@ export class Float16Schema extends Schema<number> {
}
}

export const f16 = new Float16Schema();
export const f16: Float16Schema = new Float16Schema();

////
// f32
Expand Down Expand Up @@ -312,4 +312,4 @@ export class Float32Schema extends Schema<number> {
}
}

export const f32 = new Float32Schema();
export const f32: Float32Schema = new Float32Schema();
8 changes: 5 additions & 3 deletions packages/typed-binary/src/structure/chars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
constructor(public readonly length: number) {
export class CharsSchema<
TLength extends number = number,
> extends Schema<string> {
constructor(public readonly length: TLength) {
super();
}

Expand Down Expand Up @@ -35,6 +37,6 @@ export class CharsSchema extends Schema<string> {
}
}

export function chars<T extends number>(length: T) {
export function chars<T extends number>(length: T): CharsSchema<T> {
return new CharsSchema(length);
}
2 changes: 1 addition & 1 deletion packages/typed-binary/src/structure/dynamicArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ export class DynamicArraySchema<TElement extends AnySchema> extends Schema<

export function dynamicArrayOf<TSchema extends AnySchema>(
elementSchema: TSchema,
) {
): DynamicArraySchema<TSchema> {
return new DynamicArraySchema(elementSchema);
}
2 changes: 1 addition & 1 deletion packages/typed-binary/src/structure/keyed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,6 @@ export class KeyedSchema<
export function keyed<K extends string, P extends ISchema<unknown>>(
key: K,
inner: (ref: ISchema<Ref<K>>) => P,
) {
): KeyedSchema<P, K> {
return new KeyedSchema(key, inner);
}
11 changes: 7 additions & 4 deletions packages/typed-binary/src/structure/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ export class ObjectSchema<TProps extends Record<string, AnySchema>>
}
}

export const object = <P extends Record<string, AnySchema>>(properties: P) =>
new ObjectSchema(properties);
export function object<P extends Record<string, AnySchema>>(
properties: P,
): ObjectSchema<P> {
return new ObjectSchema(properties);
}

type UnwrapGeneric<Base extends Record<string, AnySchema>, Ext> = {
[TKey in keyof Ext]: ISchema<
Expand Down Expand Up @@ -308,7 +311,7 @@ export function generic<
S extends {
[Key in keyof S]: AnySchemaWithProperties;
},
>(properties: P, subTypeMap: S) {
>(properties: P, subTypeMap: S): GenericObjectSchema<P, S> {
return new GenericObjectSchema(SubTypeKey.STRING, properties, subTypeMap);
}

Expand All @@ -317,6 +320,6 @@ export function genericEnum<
S extends {
[Key in keyof S]: AnySchemaWithProperties;
},
>(properties: P, subTypeMap: S) {
>(properties: P, subTypeMap: S): GenericObjectSchema<P, S> {
return new GenericObjectSchema(SubTypeKey.ENUM, properties, subTypeMap);
}
4 changes: 3 additions & 1 deletion packages/typed-binary/src/structure/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export class OptionalSchema<TInner extends AnySchema> extends Schema<
}
}

export function optional<TSchema extends AnySchema>(innerType: TSchema) {
export function optional<TSchema extends AnySchema>(
innerType: TSchema,
): OptionalSchema<TSchema> {
return new OptionalSchema(innerType);
}
2 changes: 1 addition & 1 deletion packages/typed-binary/src/structure/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ export class TupleSchema<

export function tupleOf<TSchema extends [AnySchema, ...AnySchema[]]>(
schemas: TSchema,
) {
): TupleSchema<TSchema> {
return new TupleSchema(schemas);
}
20 changes: 11 additions & 9 deletions packages/typed-binary/src/structure/typedArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,31 @@ export class TypedArraySchema<
}
}

export const u8Array = (length: number) =>
export const u8Array = (length: number): TypedArraySchema<Uint8Array> =>
new TypedArraySchema(length, Uint8Array);

export const u8ClampedArray = (length: number) =>
export const u8ClampedArray = (
length: number,
): TypedArraySchema<Uint8ClampedArray> =>
new TypedArraySchema(length, Uint8ClampedArray);

export const u16Array = (length: number) =>
export const u16Array = (length: number): TypedArraySchema<Uint16Array> =>
new TypedArraySchema(length, Uint16Array);

export const u32Array = (length: number) =>
export const u32Array = (length: number): TypedArraySchema<Uint32Array> =>
new TypedArraySchema(length, Uint32Array);

export const i8Array = (length: number) =>
export const i8Array = (length: number): TypedArraySchema<Int8Array> =>
new TypedArraySchema(length, Int8Array);

export const i16Array = (length: number) =>
export const i16Array = (length: number): TypedArraySchema<Int16Array> =>
new TypedArraySchema(length, Int16Array);

export const i32Array = (length: number) =>
export const i32Array = (length: number): TypedArraySchema<Int32Array> =>
new TypedArraySchema(length, Int32Array);

export const f32Array = (length: number) =>
export const f32Array = (length: number): TypedArraySchema<Float32Array> =>
new TypedArraySchema(length, Float32Array);

export const f64Array = (length: number) =>
export const f64Array = (length: number): TypedArraySchema<Float64Array> =>
new TypedArraySchema(length, Float64Array);
2 changes: 1 addition & 1 deletion packages/typed-binary/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Loading