diff --git a/src/struct.ts b/src/struct.ts index 1274829..ef85ce0 100644 --- a/src/struct.ts +++ b/src/struct.ts @@ -32,7 +32,7 @@ export interface StructOptions { } interface Member { - type: PrimitiveType | Static; + type: PrimitiveType | StructStatic; offset: number; length?: number; } @@ -45,26 +45,26 @@ interface Metadata { const metadata = Symbol('struct'); -interface Static { +export interface StructStatic { [metadata]?: Metadata; - new (): Instance; - prototype: Instance; + new (): StructInstance; + prototype: StructInstance; } -function isStatic(arg: unknown): arg is Static { +export function isStructStatic(arg: unknown): arg is StructStatic { return typeof arg == 'function' && metadata in arg; } -interface Instance { - constructor: Static; +export interface StructInstance { + constructor: StructStatic; } -function isInstance(arg: unknown): arg is Instance { +export function isStructInstance(arg: unknown): arg is StructInstance { return metadata in (arg?.constructor || {}); } -export function isStruct(arg: unknown): arg is Instance | Static { - return isInstance(arg) || isStatic(arg); +export function isStruct(arg: unknown): arg is StructInstance | StructStatic { + return isStructInstance(arg) || isStructStatic(arg); } export function sizeof(type: ValidPrimitiveType | ClassLike | object): number { @@ -96,7 +96,7 @@ export function struct(options: Partial = {}) { let size = 0; const members = new Map(); for (const { name, type, length } of target[init] as MemberInit[]) { - if (!isValidPrimitive(type) && !isStatic(type)) { + if (!isValidPrimitive(type) && !isStructStatic(type)) { throw new TypeError('Not a valid type: ' + type); } members.set(name, { @@ -127,7 +127,7 @@ export function member(type: ValidPrimitiveType | ClassLike, length?: number) { } export function serialize(instance: unknown): Uint8Array { - if (!isInstance(instance)) { + if (!isStructInstance(instance)) { throw new TypeError('Can not serialize'); } const { options, members } = instance.constructor[metadata]; @@ -165,7 +165,7 @@ export function serialize(instance: unknown): Uint8Array { } export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView) { - if (!isInstance(instance)) { + if (!isStructInstance(instance)) { throw new TypeError('Can not serialize'); } const { options, members } = instance.constructor[metadata];