Skip to content

Commit

Permalink
chore: simplify exports
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Dec 21, 2023
1 parent 67b4ff1 commit c86540b
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 202 deletions.
207 changes: 5 additions & 202 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,202 +1,5 @@
import ArrayIo, { ArrayOptions } from './type/ArrayIo.js';
import StringIo, { StringOptions } from './type/StringIo.js';
import StructIo, { StructFields, StructOptions } from './type/StructIo.js';
import TlvIo, { TlvOptions, TlvTag } from './type/TlvIo.js';
import { IoMode, validateType } from './util.js';
import { openStream } from './stream/util.js';
import { IoContext, IoSource, IoStream, IoType } from './types.js';

const array = (type: IoType, options: ArrayOptions = {}) =>
new ArrayIo(type, options);

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

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

const tlv = (
tagType: IoType,
lengthType: IoType,
valueTypes: Record<TlvTag, IoType>,
options: TlvOptions = {},
) => new TlvIo(tagType, lengthType, valueTypes, options);

const int8 = {
getSize: (_value: number) => 1,
read: (stream: IoStream) => stream.readInt8(),
write: (stream: IoStream, value: number) => stream.writeInt8(value),
};

const uint8 = {
getSize: (_value: number) => 1,
read: (stream: IoStream) => stream.readUint8(),
write: (stream: IoStream, value: number) => stream.writeUint8(value),
};

const int16 = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readInt16(),
write: (stream: IoStream, value: number) => stream.writeInt16(value),
};
const int16be = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readInt16Be(),
write: (stream: IoStream, value: number) => stream.writeInt16Be(value),
};
const int16le = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readInt16Le(),
write: (stream: IoStream, value: number) => stream.writeInt16Le(value),
};

const uint16 = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readUint16(),
write: (stream: IoStream, value: number) => stream.writeUint16(value),
};
const uint16be = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readUint16Be(),
write: (stream: IoStream, value: number) => stream.writeUint16Be(value),
};
const uint16le = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readUint16Le(),
write: (stream: IoStream, value: number) => stream.writeUint16Le(value),
};

const int32 = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readInt32(),
write: (stream: IoStream, value: number) => stream.writeInt32(value),
};
const int32be = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readInt32Be(),
write: (stream: IoStream, value: number) => stream.writeInt32Be(value),
};
const int32le = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readInt32Le(),
write: (stream: IoStream, value: number) => stream.writeInt32Le(value),
};

const uint32 = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readUint32(),
write: (stream: IoStream, value: number) => stream.writeUint32(value),
};
const uint32be = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readUint32Be(),
write: (stream: IoStream, value: number) => stream.writeUint32Be(value),
};
const uint32le = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readUint32Le(),
write: (stream: IoStream, value: number) => stream.writeUint32Le(value),
};

const int64 = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readInt64(),
write: (stream: IoStream, value: bigint) => stream.writeInt64(value),
};
const int64be = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readInt64Be(),
write: (stream: IoStream, value: bigint) => stream.writeInt64Be(value),
};
const int64le = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readInt64Le(),
write: (stream: IoStream, value: bigint) => stream.writeInt64Le(value),
};

const uint64 = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readUint64(),
write: (stream: IoStream, value: bigint) => stream.writeUint64(value),
};
const uint64be = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readUint64Be(),
write: (stream: IoStream, value: bigint) => stream.writeUint64Be(value),
};
const uint64le = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readUint64Le(),
write: (stream: IoStream, value: bigint) => stream.writeUint64Le(value),
};

const float32 = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readFloat32(),
write: (stream: IoStream, value: number) => stream.writeFloat32(value),
};
const float32be = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readFloat32Be(),
write: (stream: IoStream, value: number) => stream.writeFloat32Be(value),
};
const float32le = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readFloat32Le(),
write: (stream: IoStream, value: number) => stream.writeFloat32Le(value),
};

const float64 = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readFloat64(),
write: (stream: IoStream, value: number) => stream.writeFloat64(value),
};
const float64be = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readFloat64Be(),
write: (stream: IoStream, value: number) => stream.writeFloat64Be(value),
};
const float64le = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readFloat64Le(),
write: (stream: IoStream, value: number) => stream.writeFloat64Le(value),
};

export {
IoContext,
IoMode,
IoSource,
IoStream,
IoType,
array,
string,
struct,
tlv,
int8,
uint8,
int16,
int16be,
int16le,
uint16,
uint16be,
uint16le,
int32,
int32be,
int32le,
uint32,
uint32be,
uint32le,
int64,
int64be,
int64le,
uint64,
uint64be,
uint64le,
float32,
float32be,
float32le,
float64,
float64be,
float64le,
openStream,
validateType,
};
export * from './io/numeric.js';
export * from './io/type.js';
export { IoMode, validateType } from './util.js';
export { openStream } from './stream/util.js';
export { IoContext, IoSource, IoStream, IoType } from './types.js';
170 changes: 170 additions & 0 deletions src/lib/io/numeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import { IoStream } from '../types.js';

const int8 = {
getSize: (_value: number) => 1,
read: (stream: IoStream) => stream.readInt8(),
write: (stream: IoStream, value: number) => stream.writeInt8(value),
};

const uint8 = {
getSize: (_value: number) => 1,
read: (stream: IoStream) => stream.readUint8(),
write: (stream: IoStream, value: number) => stream.writeUint8(value),
};

const int16 = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readInt16(),
write: (stream: IoStream, value: number) => stream.writeInt16(value),
};
const int16be = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readInt16Be(),
write: (stream: IoStream, value: number) => stream.writeInt16Be(value),
};
const int16le = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readInt16Le(),
write: (stream: IoStream, value: number) => stream.writeInt16Le(value),
};

const uint16 = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readUint16(),
write: (stream: IoStream, value: number) => stream.writeUint16(value),
};
const uint16be = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readUint16Be(),
write: (stream: IoStream, value: number) => stream.writeUint16Be(value),
};
const uint16le = {
getSize: (_value: number) => 2,
read: (stream: IoStream) => stream.readUint16Le(),
write: (stream: IoStream, value: number) => stream.writeUint16Le(value),
};

const int32 = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readInt32(),
write: (stream: IoStream, value: number) => stream.writeInt32(value),
};
const int32be = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readInt32Be(),
write: (stream: IoStream, value: number) => stream.writeInt32Be(value),
};
const int32le = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readInt32Le(),
write: (stream: IoStream, value: number) => stream.writeInt32Le(value),
};

const uint32 = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readUint32(),
write: (stream: IoStream, value: number) => stream.writeUint32(value),
};
const uint32be = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readUint32Be(),
write: (stream: IoStream, value: number) => stream.writeUint32Be(value),
};
const uint32le = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readUint32Le(),
write: (stream: IoStream, value: number) => stream.writeUint32Le(value),
};

const int64 = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readInt64(),
write: (stream: IoStream, value: bigint) => stream.writeInt64(value),
};
const int64be = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readInt64Be(),
write: (stream: IoStream, value: bigint) => stream.writeInt64Be(value),
};
const int64le = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readInt64Le(),
write: (stream: IoStream, value: bigint) => stream.writeInt64Le(value),
};

const uint64 = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readUint64(),
write: (stream: IoStream, value: bigint) => stream.writeUint64(value),
};
const uint64be = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readUint64Be(),
write: (stream: IoStream, value: bigint) => stream.writeUint64Be(value),
};
const uint64le = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readUint64Le(),
write: (stream: IoStream, value: bigint) => stream.writeUint64Le(value),
};

const float32 = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readFloat32(),
write: (stream: IoStream, value: number) => stream.writeFloat32(value),
};
const float32be = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readFloat32Be(),
write: (stream: IoStream, value: number) => stream.writeFloat32Be(value),
};
const float32le = {
getSize: (_value: number) => 4,
read: (stream: IoStream) => stream.readFloat32Le(),
write: (stream: IoStream, value: number) => stream.writeFloat32Le(value),
};

const float64 = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readFloat64(),
write: (stream: IoStream, value: number) => stream.writeFloat64(value),
};
const float64be = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readFloat64Be(),
write: (stream: IoStream, value: number) => stream.writeFloat64Be(value),
};
const float64le = {
getSize: (_value: number) => 8,
read: (stream: IoStream) => stream.readFloat64Le(),
write: (stream: IoStream, value: number) => stream.writeFloat64Le(value),
};

export {
int8,
uint8,
int16,
int16be,
int16le,
uint16,
uint16be,
uint16le,
int32,
int32be,
int32le,
uint32,
uint32be,
uint32le,
int64,
int64be,
int64le,
uint64,
uint64be,
uint64le,
float32,
float32be,
float32le,
float64,
float64be,
float64le,
};
Loading

0 comments on commit c86540b

Please sign in to comment.