@chickenjdk/byteutils
Version:
Advanced tools for manipulating binary data in JavaScript
51 lines (50 loc) • 3.46 kB
TypeScript
import { AwaitedUnion } from "./types";
export declare const float32Array: Float32Array<ArrayBuffer>;
export declare const uint8Float32ArrayView: Uint8Array<ArrayBuffer>;
export declare const float64Array: Float64Array<ArrayBuffer>;
export declare const uint8Float64ArrayView: Uint8Array<ArrayBuffer>;
export declare const isBigEndian: boolean;
/**
* Join uint8arrays together
* @param arrays The uint8arrays to join
* @param totalLength The total legth of the arrays. Please provide if known as an optimization.
* If not provided, it will be calculated by summing the lengths of the arrays.
* @returns The joined uint8array
*/
export declare function joinUint8Arrays(arrays: Uint8Array[], totalLength?: number): Uint8Array;
/**
* Extend the provided readable/writable buffer to set a default endianness
* @param buffer The buffer to extend
* @param isLe If to make the default endianness Little Endian
*/
export declare function addDefaultEndianness<classType extends new (...args: any[]) => {
isLe: boolean;
}>(buffer: classType, isLe: boolean): classType;
/**
* Wrap a value for the completion of a promise
* @param awaiter The value to await (may not actualy be a promise, if not returns value with no wrappping)
* @param value The value to return
*/
export declare function wrapForPromise<awaiter extends unknown, value extends unknown>(awaiter: awaiter, value: value): awaiter extends Promise<unknown> ? Promise<value> : value;
/**
* Wrap a value for the completion of a promise
* @param awaiter The value to await (may not actualy be a promise, if not returns value with no wrappping)
* @param value The value to return
*/
export declare function wrapForAsyncCallArr<func extends (...args: args) => unknown, value extends unknown, args extends unknown[]>(func: func, params: args[], value: value): ReturnType<func> extends Promise<unknown> ? Promise<value> : value;
/**
* A funtion to help with processing values that may or may not be promises
* @param maybePromise The value that may or may not be a promise
* @param callback The callback to call with the value returned when the promise is resolved or when the value is returned directly.
* @returns Whet the callback returns, if the input is a promise, it will return a promise that resolves to the value returned by the callback.
* If the input is not a promise, it will return the value returned by the callback directly.
*/
export declare function maybePromiseThen<maybePromise, returnType>(maybePromise: maybePromise, callback: (value: maybePromise extends Promise<unknown> ? Awaited<maybePromise> : maybePromise) => returnType): maybePromise extends Promise<unknown> ? Promise<returnType> : returnType;
/**
* Call a function that may or may not return a promise for each set of parameters, if any of the calls return a promise, it will return a promise that resolves to an array of the results.
* If all calls return values directly, it will return an array of the results directly.
* @param maybeAsyncFunc A function that may or may not return a promise
* @param params The array of parameters to call the function with
* @returns The results of the calls, either as an array of values or a promise that resolves to an array of values. See main description.
*/
export declare function maybeAsyncCallArr<args extends unknown[], ret>(maybeAsyncFunc: (...args: args) => ret, params: args[]): ret extends Promise<unknown> ? Promise<AwaitedUnion<ret>[]> : ret[];