Skip to content

Commit

Permalink
Renamed things
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed May 2, 2024
1 parent c8ec453 commit 79124d5
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface StructOptions {
}

interface Member {
type: PrimitiveType | StructStatic;
type: PrimitiveType | Static;
offset: number;
length?: number;
}
Expand All @@ -45,26 +45,26 @@ interface Metadata {

const metadata = Symbol('struct');

export interface StructStatic {
interface Static {
[metadata]?: Metadata;
new (): StructInstance;
prototype: StructInstance;
new (): Instance;
prototype: Instance;
}

export function isStructStatic(arg: unknown): arg is StructStatic {
function isStatic(arg: unknown): arg is Static {
return typeof arg == 'function' && metadata in arg;
}

export interface StructInstance {
constructor: StructStatic;
interface Instance {
constructor: Static;
}

export function isStructInstance(arg: unknown): arg is StructInstance {
function isInstance(arg: unknown): arg is Instance {
return metadata in (arg?.constructor || {});
}

export function isStruct(arg: unknown): arg is StructInstance | StructStatic {
return isStructInstance(arg) || isStructStatic(arg);
export function isStruct(arg: unknown): arg is Instance | Static {
return isInstance(arg) || isStatic(arg);
}

export function sizeof(type: ValidPrimitiveType | ClassLike | object): number {
Expand Down Expand Up @@ -96,7 +96,7 @@ export function struct(options: Partial<StructOptions> = {}) {
let size = 0;
const members = new Map();
for (const { name, type, length } of target[init] as MemberInit[]) {
if (!isValidPrimitive(type) && !isStructStatic(type)) {
if (!isValidPrimitive(type) && !isStatic(type)) {
throw new TypeError('Not a valid type: ' + type);
}
members.set(name, {
Expand Down Expand Up @@ -127,7 +127,7 @@ export function member(type: ValidPrimitiveType | ClassLike, length?: number) {
}

export function serialize(instance: unknown): Uint8Array {
if (!isStructInstance(instance)) {
if (!isInstance(instance)) {
throw new TypeError('Can not serialize');
}
const { options, members } = instance.constructor[metadata];
Expand Down Expand Up @@ -165,7 +165,7 @@ export function serialize(instance: unknown): Uint8Array {
}

export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView) {
if (!isStructInstance(instance)) {
if (!isInstance(instance)) {
throw new TypeError('Can not serialize');
}
const { options, members } = instance.constructor[metadata];
Expand Down

0 comments on commit 79124d5

Please sign in to comment.