diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..ebf40ee --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,17 @@ +name: Publish + +on: + push: + branches: + - main + +jobs: + publish: + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + steps: + - uses: actions/checkout@v4 + - uses: denoland/setup-deno@v1 + - run: deno publish diff --git a/benchmarks/popular_encodings.ts b/benchmarks/popular_encodings.ts index 22d5f9c..e7de2c0 100644 --- a/benchmarks/popular_encodings.ts +++ b/benchmarks/popular_encodings.ts @@ -2,7 +2,7 @@ import { InnerType, Strings, Struct, u32, u8 } from "../mod.ts"; import { decode as msgpackRead, encode as msgpackWrite, -} from "https://deno.land/std@0.208.0/msgpack/mod.ts"; +} from "jsr:@std/msgpack@0.218"; const descriptor = { handIndex: u8, diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..da78869 --- /dev/null +++ b/deno.json @@ -0,0 +1,6 @@ +{ + "name": "@denosaurs/byte-type", + "version": "0.4.0", + "exports": "./mod.ts", + "lock": false +} diff --git a/src/array/typed_array.ts b/src/array/typed_array.ts index b55aeec..0ef1579 100644 --- a/src/array/typed_array.ts +++ b/src/array/typed_array.ts @@ -68,9 +68,13 @@ export class TypedArray extends SizedType { } } +export interface TypedArrayTypeConstructor { + new (length: number): TypedArray; +} + function createTypedArrayType>( constructor: E, -) { +): TypedArrayTypeConstructor> { return class extends TypedArray> { constructor(length: number) { super(constructor, length); @@ -78,14 +82,26 @@ function createTypedArrayType>( }; } -export const Uint8ArrayType = createTypedArrayType(Uint8Array); -export const Uint8ClampedArrayType = createTypedArrayType(Uint8ClampedArray); -export const Int8ArrayType = createTypedArrayType(Int8Array); -export const Uint16ArrayType = createTypedArrayType(Uint16Array); -export const Int16ArrayType = createTypedArrayType(Int16Array); -export const Uint32ArrayType = createTypedArrayType(Uint32Array); -export const Int32ArrayType = createTypedArrayType(Int32Array); -export const Float32ArrayType = createTypedArrayType(Float32Array); -export const Float64ArrayType = createTypedArrayType(Float64Array); -export const BigUint64ArrayType = createTypedArrayType(BigUint64Array); -export const BigInt64ArrayType = createTypedArrayType(BigInt64Array); +export const Uint8ArrayType: TypedArrayTypeConstructor = + createTypedArrayType(Uint8Array); +export const Uint8ClampedArrayType: TypedArrayTypeConstructor< + Uint8ClampedArray +> = createTypedArrayType(Uint8ClampedArray); +export const Int8ArrayType: TypedArrayTypeConstructor = + createTypedArrayType(Int8Array); +export const Uint16ArrayType: TypedArrayTypeConstructor = + createTypedArrayType(Uint16Array); +export const Int16ArrayType: TypedArrayTypeConstructor = + createTypedArrayType(Int16Array); +export const Uint32ArrayType: TypedArrayTypeConstructor = + createTypedArrayType(Uint32Array); +export const Int32ArrayType: TypedArrayTypeConstructor = + createTypedArrayType(Int32Array); +export const Float32ArrayType: TypedArrayTypeConstructor = + createTypedArrayType(Float32Array); +export const Float64ArrayType: TypedArrayTypeConstructor = + createTypedArrayType(Float64Array); +export const BigUint64ArrayType: TypedArrayTypeConstructor = + createTypedArrayType(BigUint64Array); +export const BigInt64ArrayType: TypedArrayTypeConstructor = + createTypedArrayType(BigInt64Array); diff --git a/src/primitives/bool.ts b/src/primitives/bool.ts index 183c4a9..c7b5e03 100644 --- a/src/primitives/bool.ts +++ b/src/primitives/bool.ts @@ -25,4 +25,4 @@ export class Bool extends SizedType { } } -export const bool = new Bool(); +export const bool: Bool = new Bool(); diff --git a/src/primitives/f32.ts b/src/primitives/f32.ts index 6e730ea..1ffc008 100644 --- a/src/primitives/f32.ts +++ b/src/primitives/f32.ts @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts"; import { isLittleEndian } from "../util.ts"; export class F32 extends SizedType { - constructor(readonly littleEndian = isLittleEndian) { + constructor(readonly littleEndian: boolean = isLittleEndian) { super(4, 4); } @@ -24,6 +24,6 @@ export class F32 extends SizedType { } } -export const f32le = new F32(true); -export const f32be = new F32(false); -export const f32 = new F32(); +export const f32le: F32 = new F32(true); +export const f32be: F32 = new F32(false); +export const f32: F32 = new F32(); diff --git a/src/primitives/f64.ts b/src/primitives/f64.ts index 4d0bdd0..1fa1695 100644 --- a/src/primitives/f64.ts +++ b/src/primitives/f64.ts @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts"; import { isLittleEndian } from "../util.ts"; export class F64 extends SizedType { - constructor(readonly littleEndian = isLittleEndian) { + constructor(readonly littleEndian: boolean = isLittleEndian) { super(8, 8); } @@ -24,6 +24,6 @@ export class F64 extends SizedType { } } -export const f64le = new F64(true); -export const f64be = new F64(false); -export const f64 = new F64(); +export const f64le: F64 = new F64(true); +export const f64be: F64 = new F64(false); +export const f64: F64 = new F64(); diff --git a/src/primitives/i16.ts b/src/primitives/i16.ts index 3f43ae4..b43f563 100644 --- a/src/primitives/i16.ts +++ b/src/primitives/i16.ts @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts"; import { isLittleEndian } from "../util.ts"; export class I16 extends SizedType { - constructor(readonly littleEndian = isLittleEndian) { + constructor(readonly littleEndian: boolean = isLittleEndian) { super(2, 2); } @@ -24,6 +24,6 @@ export class I16 extends SizedType { } } -export const i16le = new I16(true); -export const i16be = new I16(false); -export const i16 = new I16(); +export const i16le: I16 = new I16(true); +export const i16be: I16 = new I16(false); +export const i16: I16 = new I16(); diff --git a/src/primitives/i32.ts b/src/primitives/i32.ts index 995a94d..d56adb4 100644 --- a/src/primitives/i32.ts +++ b/src/primitives/i32.ts @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts"; import { isLittleEndian } from "../util.ts"; export class I32 extends SizedType { - constructor(readonly littleEndian = isLittleEndian) { + constructor(readonly littleEndian: boolean = isLittleEndian) { super(4, 4); } @@ -24,6 +24,6 @@ export class I32 extends SizedType { } } -export const i32le = new I32(true); -export const i32be = new I32(false); -export const i32 = new I32(); +export const i32le: I32 = new I32(true); +export const i32be: I32 = new I32(false); +export const i32: I32 = new I32(); diff --git a/src/primitives/i64.ts b/src/primitives/i64.ts index d807ea3..513d267 100644 --- a/src/primitives/i64.ts +++ b/src/primitives/i64.ts @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts"; import { isLittleEndian } from "../util.ts"; export class I64 extends SizedType { - constructor(readonly littleEndian = isLittleEndian) { + constructor(readonly littleEndian: boolean = isLittleEndian) { super(8, 8); } @@ -24,6 +24,6 @@ export class I64 extends SizedType { } } -export const i64le = new I64(true); -export const i64be = new I64(false); -export const i64 = new I64(); +export const i64le: I64 = new I64(true); +export const i64be: I64 = new I64(false); +export const i64: I64 = new I64(); diff --git a/src/primitives/i8.ts b/src/primitives/i8.ts index 7df56ec..635951a 100644 --- a/src/primitives/i8.ts +++ b/src/primitives/i8.ts @@ -23,4 +23,4 @@ export class I8 extends SizedType { } } -export const i8 = new I8(); +export const i8: I8 = new I8(); diff --git a/src/primitives/u16.ts b/src/primitives/u16.ts index 1a56b9a..32ac527 100644 --- a/src/primitives/u16.ts +++ b/src/primitives/u16.ts @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts"; import { isLittleEndian } from "../util.ts"; export class U16 extends SizedType { - constructor(readonly littleEndian = isLittleEndian) { + constructor(readonly littleEndian: boolean = isLittleEndian) { super(2, 2); } @@ -24,6 +24,6 @@ export class U16 extends SizedType { } } -export const u16le = new U16(true); -export const u16be = new U16(false); -export const u16 = new U16(); +export const u16le: U16 = new U16(true); +export const u16be: U16 = new U16(false); +export const u16: U16 = new U16(); diff --git a/src/primitives/u32.ts b/src/primitives/u32.ts index fe78460..32016af 100644 --- a/src/primitives/u32.ts +++ b/src/primitives/u32.ts @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts"; import { isLittleEndian } from "../util.ts"; export class U32 extends SizedType { - constructor(readonly littleEndian = isLittleEndian) { + constructor(readonly littleEndian: boolean = isLittleEndian) { super(4, 4); } @@ -24,6 +24,6 @@ export class U32 extends SizedType { } } -export const u32le = new U32(true); -export const u32be = new U32(false); -export const u32 = new U32(); +export const u32le: U32 = new U32(true); +export const u32be: U32 = new U32(false); +export const u32: U32 = new U32(); diff --git a/src/primitives/u64.ts b/src/primitives/u64.ts index 0279b0b..f7bf151 100644 --- a/src/primitives/u64.ts +++ b/src/primitives/u64.ts @@ -2,7 +2,7 @@ import { type Options, SizedType } from "../types/mod.ts"; import { isLittleEndian } from "../util.ts"; export class U64 extends SizedType { - constructor(readonly littleEndian = isLittleEndian) { + constructor(readonly littleEndian: boolean = isLittleEndian) { super(8, 8); } @@ -24,6 +24,6 @@ export class U64 extends SizedType { } } -export const u64le = new U64(true); -export const u64be = new U64(false); -export const u64 = new U64(); +export const u64le: U64 = new U64(true); +export const u64be: U64 = new U64(false); +export const u64: U64 = new U64(); diff --git a/src/primitives/u8.ts b/src/primitives/u8.ts index 340ff2b..63156e7 100644 --- a/src/primitives/u8.ts +++ b/src/primitives/u8.ts @@ -23,4 +23,4 @@ export class U8 extends SizedType { } } -export const u8 = new U8(); +export const u8: U8 = new U8(); diff --git a/src/small_number/mod.ts b/src/small_number/mod.ts index c4321f2..07e602a 100644 --- a/src/small_number/mod.ts +++ b/src/small_number/mod.ts @@ -45,5 +45,5 @@ export class U4 extends SizedType { } } -export const u2 = new U2(); -export const u4 = new U4(); +export const u2: U2 = new U2(); +export const u4: U4 = new U4(); diff --git a/src/string/cstring.ts b/src/string/cstring.ts index ae3c40f..a041883 100644 --- a/src/string/cstring.ts +++ b/src/string/cstring.ts @@ -35,4 +35,4 @@ export class CString extends UnsizedType { } } -export const cstring = new CString(); +export const cstring: CString = new CString(); diff --git a/src/string/fixed_length.ts b/src/string/fixed_length.ts index da1e290..a4f79ba 100644 --- a/src/string/fixed_length.ts +++ b/src/string/fixed_length.ts @@ -38,5 +38,5 @@ export class FixedLengthString extends SizedType { } } -export const asciiChar = new FixedLengthString(1); -export const utf8Char = new FixedLengthString(4); +export const asciiChar: FixedLengthString = new FixedLengthString(1); +export const utf8Char: FixedLengthString = new FixedLengthString(4); diff --git a/src/util.ts b/src/util.ts index 65d928a..92b3e29 100644 --- a/src/util.ts +++ b/src/util.ts @@ -3,14 +3,14 @@ import type { UnsizedType } from "./mod.ts"; /** * The endianess of your machine, true if little endian and false if big endian. */ -export const isLittleEndian = (() => { +export const isLittleEndian: boolean = (() => { const buffer = new ArrayBuffer(2); new DataView(buffer).setUint16(0, 256, true); return new Uint16Array(buffer)[0] === 256; })(); /** Align the value `unaligned` to the first integer that is divisible by `alignment` */ -export const align = (unaligned: number, alignment: number) => +export const align = (unaligned: number, alignment: number): number => (unaligned + alignment - 1) & ~(alignment - 1); type ArrayOrRecord = T[] | Record; @@ -18,6 +18,6 @@ type ArrayOrRecord = T[] | Record; /** Find and returns the biggest alignment out of a record / array of types */ export const getBiggestAlignment = ( input: ArrayOrRecord>, -) => +): number => Object.values(input) .reduce((acc, x) => Math.max(acc, x.byteAlignment), 0); diff --git a/src/varint/i32_leb128.ts b/src/varint/i32_leb128.ts index 0f3c5a9..9006894 100644 --- a/src/varint/i32_leb128.ts +++ b/src/varint/i32_leb128.ts @@ -43,4 +43,4 @@ export class I32Leb128 extends UnsizedType { } } -export const i32leb128 = new I32Leb128(); +export const i32leb128: I32Leb128 = new I32Leb128(); diff --git a/src/varint/i64_leb128.ts b/src/varint/i64_leb128.ts index 5a85dd5..ee8847c 100644 --- a/src/varint/i64_leb128.ts +++ b/src/varint/i64_leb128.ts @@ -87,4 +87,4 @@ export class I64Leb128 extends UnsizedType { } } -export const i64leb128 = new I64Leb128(); +export const i64leb128: I64Leb128 = new I64Leb128(); diff --git a/test_deps.ts b/test_deps.ts index 1a66e3e..9d52d9f 100644 --- a/test_deps.ts +++ b/test_deps.ts @@ -1 +1 @@ -export * from "https://deno.land/std@0.204.0/assert/mod.ts"; +export * from "jsr:@std/assert@0.218";