Comparing version 0.3.0 to 0.3.1
import { ClassLike } from './types.js'; | ||
export type PrimitiveType = `${'int' | 'uint'}${8 | 16 | 32 | 64}` | `float${32 | 64}`; | ||
export type ValidPrimitiveType = PrimitiveType | Capitalize<PrimitiveType> | 'char'; | ||
/** | ||
* Options for struct initialization | ||
*/ | ||
export interface StructOptions { | ||
@@ -8,8 +11,31 @@ align: number; | ||
} | ||
/** | ||
* Gets the size in bytes of a type | ||
*/ | ||
export declare function sizeof(type: ValidPrimitiveType | ClassLike | object): number; | ||
/** | ||
* Aligns a number | ||
*/ | ||
export declare function align(value: number, alignment: number): number; | ||
/** | ||
* Decorates a class as a struct | ||
*/ | ||
export declare function struct(options?: Partial<StructOptions>): (target: ClassLike, _?: ClassDecoratorContext) => void; | ||
/** | ||
* Decorates a class member to be serialized | ||
*/ | ||
export declare function member(type: ValidPrimitiveType | ClassLike, length?: number): (target: object, context?: ClassMemberDecoratorContext | string | symbol) => void; | ||
/** | ||
* Serializes a struct into a Uint8Array | ||
*/ | ||
export declare function serialize(instance: unknown): Uint8Array; | ||
export declare function deserialize(instance: unknown, _buffer: Uint8Array): void; | ||
/** | ||
* Deserializes a struct from a Uint8Array | ||
*/ | ||
export declare function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView): void; | ||
/** | ||
* Shortcut types | ||
* | ||
* Instead of writing `@member(type)` you can write `@types.type`, or `@types.type(length)` for arrays | ||
*/ | ||
export declare const types: { | ||
@@ -16,0 +42,0 @@ int8: { |
@@ -25,2 +25,5 @@ import { capitalize } from './string.js'; | ||
} | ||
/** | ||
* Gets the size in bytes of a type | ||
*/ | ||
export function sizeof(type) { | ||
@@ -40,5 +43,11 @@ // primitive | ||
} | ||
/** | ||
* Aligns a number | ||
*/ | ||
export function align(value, alignment) { | ||
return Math.ceil(value / alignment) * alignment; | ||
} | ||
/** | ||
* Decorates a class as a struct | ||
*/ | ||
export function struct(options = {}) { | ||
@@ -66,2 +75,5 @@ // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
} | ||
/** | ||
* Decorates a class member to be serialized | ||
*/ | ||
export function member(type, length) { | ||
@@ -79,2 +91,5 @@ return function (target, context) { | ||
} | ||
/** | ||
* Serializes a struct into a Uint8Array | ||
*/ | ||
export function serialize(instance) { | ||
@@ -113,2 +128,5 @@ if (!isInstance(instance)) { | ||
} | ||
/** | ||
* Deserializes a struct from a Uint8Array | ||
*/ | ||
export function deserialize(instance, _buffer) { | ||
@@ -162,2 +180,7 @@ if (!isInstance(instance)) { | ||
} | ||
/** | ||
* Shortcut types | ||
* | ||
* Instead of writing `@member(type)` you can write `@types.type`, or `@types.type(length)` for arrays | ||
*/ | ||
export const types = Object.fromEntries(validPrimitiveTypes.map(t => [t, _member(t)])); |
{ | ||
"name": "utilium", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Typescript utilies", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -33,2 +33,5 @@ import { capitalize } from './string.js'; | ||
/** | ||
* Options for struct initialization | ||
*/ | ||
export interface StructOptions { | ||
@@ -75,2 +78,5 @@ align: number; | ||
/** | ||
* Gets the size in bytes of a type | ||
*/ | ||
export function sizeof(type: ValidPrimitiveType | ClassLike | object): number { | ||
@@ -94,2 +100,5 @@ // primitive | ||
/** | ||
* Aligns a number | ||
*/ | ||
export function align(value: number, alignment: number): number { | ||
@@ -99,2 +108,5 @@ return Math.ceil(value / alignment) * alignment; | ||
/** | ||
* Decorates a class as a struct | ||
*/ | ||
export function struct(options: Partial<StructOptions> = {}) { | ||
@@ -124,2 +136,5 @@ // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
/** | ||
* Decorates a class member to be serialized | ||
*/ | ||
export function member(type: ValidPrimitiveType | ClassLike, length?: number) { | ||
@@ -138,2 +153,5 @@ return function (target: object, context?: ClassMemberDecoratorContext | string | symbol) { | ||
/** | ||
* Serializes a struct into a Uint8Array | ||
*/ | ||
export function serialize(instance: unknown): Uint8Array { | ||
@@ -181,3 +199,6 @@ if (!isInstance(instance)) { | ||
export function deserialize(instance: unknown, _buffer: Uint8Array) { | ||
/** | ||
* Deserializes a struct from a Uint8Array | ||
*/ | ||
export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView) { | ||
if (!isInstance(instance)) { | ||
@@ -245,2 +266,7 @@ throw new TypeError('Can not deserialize, not a struct instance'); | ||
/** | ||
* Shortcut types | ||
* | ||
* Instead of writing `@member(type)` you can write `@types.type`, or `@types.type(length)` for arrays | ||
*/ | ||
export const types = Object.fromEntries(validPrimitiveTypes.map(t => [t, _member(t)])) as { [K in ValidPrimitiveType]: ReturnType<typeof _member<K>> }; |
54197
1551