streamstructure
Version:
Parses objects into very compact buffer
83 lines • 3.41 kB
TypeScript
/// <reference types="node" />
declare class StreamStructure {
structure: string[];
private endian;
private typesDefinitions;
private typeConditions;
private preProcessing;
private posProcessing;
static readonly primitivesLength: Readonly<{
readonly boolean: 1;
readonly char: 1;
readonly string: 2;
readonly byte: 1;
readonly ubyte: 1;
readonly short: 2;
readonly ushort: 2;
readonly int: 4;
readonly uint: 4;
readonly long: 8;
readonly ulong: 8;
readonly float: 4;
readonly double: 8;
}>;
/** Picks the value from `(key): (value[size])` */
private static readonly typeObjectReader;
/** Picks the value from `(value)([size])` */
private static readonly typeReader;
/** Breaks the array size from `[(!)(size1)]([size2][size3])` */
private static readonly typeArrayBreaker;
/**
* Create a StreamStructure, must be created using the sequence `key: type`
*
* @example //Creating a structure for a simple object `{name: string,age: number}`
* cosnt SS = new StreamStructure("name: string", "age: byte");
*/
constructor(...types: string[]);
toBuffer(data: Record<string, unknown>): Buffer;
fromBuffer(buffer: Buffer): Record<string, unknown>;
/**
* Creates a Complex type, maded of anothers types.
*
* @param type the type that will be created
* @param structure a sequence of `key: type`
*/
setType(type: string, ...structure: string[]): this;
/**
* Creates a pre-process and post-process for any type, userful for get a better reading out or input.
* @param type the type that will be pre-processed and post-processed
* @param preProcessing the pre-processor used to change this type when is going to transform into buffer
* @param postProcessing the post-processor used to change this type when is going to transform from buffer
*/
setTypeProcess<A, B>(type: string, preProcessing: (value: A) => B, postProcessing: (value: B) => A): this;
/**
* Sets the type of key from a typeConditional, normally used only with "string" or "byte".
* @param type
* @param indexType
* @returns
*/
setTypeConditionalIndex(type: string, indexType: string): this;
/**
* Creates a type that changes the structure based on the key setted before, usefull for recursive objects
* @param type the type to be created
* @param condition if the key is equal to this argument, will use this structure
* @param structure structure to be used
*/
setTypeConditional(type: string, condition: string | number | symbol, ...structure: string[]): this;
/**
* @deprecated mismatch :P, instead use the `SS.setTypeConditionalIndex()`
*/
setTypeCondicionalIndex(type: string, indexType: string): this;
/**
* @deprecated mismatch :P, instead use the `SS.setTypeConditional()`
*/
setTypeCondicional(type: string, condition: string, structure: string[]): this;
/**
* Set the default endian for the numbers, arrays, etc.
* @param endian the default endian
* @returns
*/
setDefaultEndian(endian: "BE" | "LE"): this;
}
export = StreamStructure;
//# sourceMappingURL=index.d.ts.map