Skip to content

Commit

Permalink
chore: clean up types
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Nov 25, 2023
1 parent ecdf880 commit 3dcb6b4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
14 changes: 8 additions & 6 deletions src/lib/index.mts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import ArrayIo from './type/ArrayIo.mjs';
import StringIo from './type/StringIo.mjs';
import StructIo from './type/StructIo.mjs';
import ArrayIo, { ArrayOptions } from './type/ArrayIo.mjs';
import StringIo, { StringOptions } from './type/StringIo.mjs';
import StructIo, { StructFields, StructOptions } from './type/StructIo.mjs';
import TlvIo, { TlvValueCallback } from './type/TlvIo.mjs';
import { getType } from './util.mjs';

const array = (type, options) => new ArrayIo(type, options);
const array = (type: Function | IoType, options: ArrayOptions) =>
new ArrayIo(type, options);

const string = (options) => new StringIo(options);
const string = (options: StringOptions) => new StringIo(options);

const struct = (fields, options) => new StructIo(fields, options);
const struct = (fields: StructFields, options: StructOptions) =>
new StructIo(fields, options);

const tlv = (
tagType: IoType,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/type/ArrayIo.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ArrayIo implements IoType {
#type: IoType;
#options: ArrayOptions;

constructor(type: IoType, options: ArrayOptions = {}) {
constructor(type: Function | IoType, options: ArrayOptions = {}) {
if (type === undefined) {
throw new Error('Missing required argument: type');
}
Expand Down Expand Up @@ -65,3 +65,4 @@ class ArrayIo implements IoType {
}

export default ArrayIo;
export { ArrayOptions };
1 change: 1 addition & 0 deletions src/lib/type/StringIo.mts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ class StringIo implements IoType {
}

export default StringIo;
export { StringOptions };
15 changes: 11 additions & 4 deletions src/lib/type/StructIo.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { getStream, getType } from '../util.mjs';
import { Endianness, getStream, getType } from '../util.mjs';

type StructFields = Record<string, Function | IoType>;

type StructOptions = {
endianness?: Endianness;
};

class StructIo implements IoType {
#fields;
#options;
#fields: StructFields;
#options: StructOptions;

constructor(fields = {}, options = {}) {
constructor(fields: StructFields = {}, options: StructOptions = {}) {
this.#fields = fields;
this.#options = options;
}
Expand Down Expand Up @@ -57,3 +63,4 @@ class StructIo implements IoType {
}

export default StructIo;
export { StructFields, StructOptions };
16 changes: 7 additions & 9 deletions src/lib/util.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@ enum Endianness {
Big = 2,
}

const getStream = (source, endianness = Endianness.Little): Stream =>
isStream(source) ? source : openStream(source, endianness);
const getStream = (source: Source, endianness = Endianness.Little): Stream =>
isStream(source) ? (source as Stream) : openStream(source, endianness);

const getType = (type): IoType => {
if (typeof type === 'function') {
type = type();
}
const getType = (type: Function | IoType): IoType => {
const resolvedType = typeof type === 'function' ? type() : type;

if (typeof type.read !== 'function') {
if (typeof resolvedType.read !== 'function') {
throw new Error('Missing required function: read');
}

return type;
return resolvedType;
};

const resolveValue = (ref, ...objects) => {
const resolveValue = (ref: number | string, ...objects: object[]) => {
if (ref === undefined || typeof ref === 'number') {
return ref;
}
Expand Down

0 comments on commit 3dcb6b4

Please sign in to comment.