@emmveqz/js-utils
Version:
Utilities for JavaScript
36 lines (35 loc) • 1.83 kB
TypeScript
import { IArrayEl, IAsyncTryCatch, ICtxAsyncTryCatch, ICtxTryCatch, IEnumLike, IFuncArgs, ITryCatch, IValOf, IWithAsyncTryCatch, IWithTryCatch, MyAsyncGenerator } from '../types';
/**
* This relies blindly that `Promise.race(promises)` will resolve with `promises[0]`
*
* even if all `promises` were resolved when passed.
*/
export declare const getPromiseState: <T>(promise: PromiseLike<T>) => Promise<"pending" | "fulfilled" | "rejected">;
/**
* Returns an iterator, yielding the value of `'fulfilled'` promises first.
*
* In case a promise is rejected, the iterator will yield an `Error` instance instead.
*/
export declare function racePromisesIterator<T>(promises: PromiseLike<T>[]): MyAsyncGenerator<T | Error>;
export declare const arrayUniqueByProp: <T extends Record<string, unknown>>(arr: Array<T>, prop: keyof T) => Array<T>;
/**
* If you are not certain of the types for the `arr` values,
*
* consider validating the result with `Number.isNaN(result)`
*/
export declare const arraySum: <T extends Array<unknown>>(arr: T) => IArrayEl<T> extends number ? number : never;
/**
* Returns a copy of the `arr` which halts at the first `false` occurence for `predicate`.
*/
export declare const arrayFilterHalt: <T>(arr: Array<T>, predicate: IFuncArgs<Array<T>["findIndex"]>[0]) => Array<T>;
/**
* Consider using `new Set(arr)`
*/
export declare const arrayUnique: <T>(arr: Array<T>) => Array<T>;
export declare const extractEnumNumbers: <T extends IEnumLike>(en: T) => Array<IValOf<T, string>>;
export declare const tryCatch: ITryCatch;
export declare const ctxTryCatch: ICtxTryCatch;
export declare const asyncTryCatch: IAsyncTryCatch;
export declare const ctxAsyncTryCatch: ICtxAsyncTryCatch;
export declare const withTryCatch: IWithTryCatch;
export declare const withAsyncTryCatch: IWithAsyncTryCatch;