@1k/shared

v0.1.24

Published

Utilities shared across @1k packages

Downloads

1,307

Readme

@1k/shared

types

const eq: <T>(value: T, other?: unknown) => other is T;

const kindOf: <T>(thing: T) => string;

const isArguments: (it?: unknown) => it is IArguments;

const isArray: <T>(it: unknown) => it is T[];

const isArrayBuffer: (value?: unknown) => value is ArrayBuffer;

const isArrayBufferView: (it: unknown) => it is ArrayBufferView;

const isArrayLike: (value: unknown) => value is ArrayLike<unknown>;

const isArrayLikeObject: (value: unknown) => value is ArrayLike<unknown> & object;

const isBigInt: (it: unknown) => it is bigint;

const isBlob: (x: unknown) => x is Blob;

const isBoolean: (x: unknown) => x is boolean;

const isDate: (x: unknown) => x is Date;

const isDefined: <const T>(it: T | undefined) => it is Exclude<T, undefined>;

const isError: (x: unknown) => x is Error;

const isEven: (x: unknown) => x is number;

type Falsy<T> = T & (false | 0 | 0n | '' | null | undefined);

const isFalsy: <T>(x: T) => x is Falsy<T>;

const isFile: (x: unknown) => x is File;

const isFinite: (n: unknown) => n is number;

const isFunction: (value: unknown) => value is Function;

const isInteger: (n: unknown) => n is number;

type JsonValue = string | number | boolean | null | JsonObject | JsonArray;

type JsonArray = JsonValue[];

type JsonObject = {
    [key: string]: JsonValue;
};

type EqualityComparer<T, U = T> = (a: T, b: U) => boolean;

function isJsonValue(value: unknown): value is JsonValue;

function isJsonArray(value: unknown): value is JsonArray;

function isJsonObject(obj: unknown): obj is JsonObject;

const isLength: (value: unknown) => value is number;

const isMap: (it: unknown) => it is Map<unknown, unknown>;

const isNil: (it: unknown) => it is null | undefined;

type NAN = typeof NaN;

const isNaN: (x: unknown) => x is NAN;

const isNative: (value: unknown) => value is Function;

function isNegative<const N>(it: N): it is number & N;

const isNull: (x: unknown) => x is null;

const isNumber: (v: unknown) => v is number;

const isNumeric: (n: unknown) => n is string | number;

const isObject: <T>(it: unknown) => it is Record<PropertyKey, T>;

function isObjectLike(value: unknown): value is object;

const isOdd: (x: unknown) => x is number;

const isPlainObject: (value: unknown) => value is Record<PropertyKey, any>;

function isPositive<const N>(it: N): it is number & N;

const isPresent: <T>(it: T | null | undefined) => it is NonNullable<T>;

const isPrimitive: (value: unknown) => value is null | undefined | string | number | boolean | symbol | bigint;

type Printable = string | number | boolean | bigint | null | undefined;

const isPrintable: (x: unknown) => x is Printable;

const isPromise: <T>(it: unknown) => it is Promise<T>;

const isPromiseLike: <T>(x: unknown) => x is PromiseLike<T>;

const isRegExp: <T>(x: T) => x is T & RegExp;

const isSafeInteger: (n: unknown) => n is number;

const isScalar: (it: unknown) => it is string | number | boolean;

const isSet: <T>(it: unknown) => it is Set<T>;

const isString: (it: unknown) => it is string;

const isSymbol: (it: unknown) => it is symbol;

type Truthy<T> = Exclude<T, false | 0 | 0n | '' | null | undefined>;

const isTruthy: <T>(it: T) => it is Truthy<T>;

const isTypedArray: (x: unknown) => x is Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | BigUint64Array | Int8Array | Int16Array | Int32Array | BigInt64Array | Float32Array | Float64Array;

const isUndefined: (it: unknown) => it is undefined;

const isUrlSearchParams: (x: unknown) => x is URLSearchParams;

const isWeakMap: (value: unknown) => value is WeakMap<WeakKey, unknown>;

const isWeakSet: (value: unknown) => value is WeakSet<WeakKey>;

const isWellFormed: (str: string) => boolean;

const isWindow: (it: unknown) => it is Window;

const now: () => number;

const chunk: <T>(arr: readonly T[], size: number) => T[][];

const compact: <T>(arr: ArrayLike<T>) => Truthy<T>[];

const countBy: <T, K extends PropertyKey>(
    list: ArrayLike<T>, getKey: (item: T, i: number, list: ArrayLike<T>) => K) => Record<K, number>;

const drop: <T>(arr: readonly T[], itemsCount: number) => T[];

const dropLast: <T>(arr: readonly T[], itemsCount: number) => T[];

const dropLastWhile: <T>(
    list: readonly T[], canContinueDropping: (item: T, index: number, arr: readonly T[]) => boolean) => T[];

const dropWhile: <T>(arr: readonly T[], canContinueDropping: (item: T, index: number, arr: readonly T[]) => boolean) => T[];

function fill<T>(array: unknown[], value: T): T[];
function fill<T, U>(array: Array<T | U>, value: U, start: number): Array<T | U>;
function fill<T, U>(array: Array<T | U>, value: U, start: number, end: number): Array<T | U>;

function first<T>(arr: readonly [T, ...T[]]): T;
function first<T>(arr: readonly T[]): T | undefined;

const groupBy: <T, K extends PropertyKey>(
    list: ArrayLike<T>, getKeyFromItem: (item: T, i: number, list: ArrayLike<T>) => K) => Record<K, T[]>;

interface Partition {
    <U, T extends U = U>(
    list: ArrayLike<U>, isTruthy: (item: U, i: number, list: ArrayLike<U>) => item is T): [truthy: T[], falsy: Exclude<U, T>[]];

    <T>(
    list: ArrayLike<T>, isTruthy: (item: T, i: number, list: ArrayLike<T>) => boolean): [truthy: T[], falsy: T[]];

}
const partition: Partition;

const keyBy: <T, K extends PropertyKey>(
    list: ArrayLike<T>, getKeyFromItem: (item: T, i: number, list: ArrayLike<T>) => K) => Record<K, T>;

function last<T>(arr: readonly [...T[], T]): T;
function last<T>(arr: readonly T[]): T | undefined;

function maxBy<T>(items: readonly [T, ...T[]], getValue: (el: T, i: number, items: ArrayLike<T>) => number): T;

function maxBy<T>(items: ArrayLike<T>, getValue: (el: T, i: number, items: ArrayLike<T>) => number): T | undefined;

function minBy<T>(items: readonly [T, ...T[]], getValue: (el: T, i: number, items: ArrayLike<T>) => number): T;

function minBy<T>(items: ArrayLike<T>, getValue: (el: T, i: number, items: ArrayLike<T>) => number): T | undefined;

const sample: <T>(arr: readonly T[]) => T;

const shuffle: <T>(arr: readonly T[]) => T[];

const take: <T>(arr: readonly T[], count: number) => T[];

const takeLast: <T>(arr: readonly T[], count?: number) => T[];

const takeLastWhile: <T>(arr: readonly T[], shouldContinueTaking: (item: T) => boolean) => T[];

const takeWhile: <T>(arr: readonly T[], shouldContinueTaking: (element: T) => boolean) => T[];

function contains<T, U = T>(array: ArrayLike<T>, value: U, equalityComparer?: EqualityComparer<T, U>): boolean;

function every<T, U extends T>(
    list: T[],
    predicate: (element: T, index: number, list: ArrayLike<T>) => element is U
): list is U[];
function every<T, U extends T>(
    list: readonly T[],
    predicate: (element: T, index: number, list: ArrayLike<T>) => element is U
): list is readonly U[];
function every<T>(
    list: ArrayLike<T>,
    predicate: (element: T, index: number, list: ArrayLike<T>) => boolean
): boolean;

function filter<T, U extends T>(
    array: ArrayLike<T> | null | undefined,
    f: (x: T, i: number, list: ArrayLike<T>) => x is U,
    startIndex?: number,
    limit?: number ): U[];
function filter<T>(
    list: ArrayLike<T> | null | undefined,
    f: (x: T, i: number, list: ArrayLike<T>) => boolean,
    startIndex?: number,
    limit?: number ): T[];

function find<T, U extends T>(
    array: readonly T[] | null | undefined,
    predicate: (value: T, index: number,
    list: ArrayLike<T>) => value is U,
    startIndex?: number,
    limit?: number ): U | undefined;

function find<T>(
    list: ArrayLike<T> | null | undefined,
    predicate: (value: T, index: number, list: ArrayLike<T>) => boolean,
    startIndex?: number,
    limit?: number ): T | undefined;

const findIndex: <T>(
    list: ArrayLike<T> | null | undefined,
    predicate: (v: T, i: number, a: ArrayLike<T>) => boolean,
    startIndex?: number,
    limit?: number ) => number;

function findLast<T, U extends T>(
    array: readonly T[] | null | undefined,
    predicate: (value: T, index: number, list: ArrayLike<T>) => value is U,
    startIndex?: number,
    limit?: number ): U | undefined;

function findLast<T>(
    list: ArrayLike<T> | null | undefined,
    predicate: (v: T, i: number, list: ArrayLike<T>) => boolean,
    startIndex?: number,
    limit?: number ): T | undefined;

const findLastIndex: <T>(
    list: ArrayLike<T> | null | undefined,
    predicate: (v: T, i: number, a: ArrayLike<T>) => boolean,
    startIndex?: number,
    limit?: number ) => number;

function each<T>(
    list: ArrayLike<T> | null | undefined,
    iterate: (element: T, index: number, list: ArrayLike<T>) => unknown,
    startIndex?: number,
    limit?: number): ArrayLike<T> | null | undefined;

function eachLast<T>(
    list: ArrayLike<T> | null | undefined,
    iterate: (element: T, index: number, list: ArrayLike<T>) => unknown,
    startIndex?: number,
    limit?: number): ArrayLike<T> | null | undefined;

const includes: <T>(
    list: readonly T[] | { includes(el: T, fromIndex?: number): boolean; },
    el: T,
    fromIndex?: number) => boolean;

const indexOf: <T>(
    list: ArrayLike<T> | string | null | undefined,
    value: T | string,
    startIndex?: number) => number;

const lastIndexOf: <T>(
    list: ArrayLike<T> | string | undefined | null, value: T | string, fromIndex?: number) => number;

function map<T>(
    list: ArrayLike<T>): T[];

function map<T, U extends T>(array: readonly T[], mapFn: (x: T, i: number, list: ArrayLike<T>) => U, startIndex?: number, limit?: number): readonly U[];

function map<T, U>(
    list: ArrayLike<T>, mapFn: (x: T, i: number, list: ArrayLike<T>) => U, startIndex?: number, limit?: number): U[];

function reduce<T, U = T>(array: readonly T[], f: (memo: U, value: T, i: number) => U, initial: U, start?: number, count?: number): U;

function reduce<T>(array: readonly T[], f: (memo: T, value: T, i: number) => T, initial: T): T;

function reduce<T>(array: readonly T[], f: (memo: T, value: T, i: number) => T): T | undefined;

function reject<T>(
    list: ArrayLike<T>,
    f: (x: T, i: number, list: ArrayLike<T>) => boolean,
    startIndex?: number,
    limit?: number): T[];

function some<T>(
    list: ArrayLike<T>,
    predicate: (value: T, index: number, list: ArrayLike<T>) => boolean
): boolean;

const after: <A extends unknown[], R>(
    n: number,
    func: (...args: A) => R
) => (...args: A) => R | undefined;

const ary: <A extends unknown[], R, This = unknown>(
    func: (this: This, ...args: A) => R, n: number
) => (this: This, ...args: A) => R;

const attempt: <A extends unknown[], R>(
    func: (...args: A) => R, ...args: A
) => R | Error;

const always: <const T>(value: T) => () => T;

const alwaysTrue: () => true;

const alwaysFalse: () => false;

const before: <A extends unknown[], R>(
    n: number,
    func: (...args: A) => R
) => (...args: A) => R | undefined;

interface DebounceOptions {
    signal?: AbortSignal;
    edges?: Array<'leading' | 'trailing'>;
}

const debounce: <A extends unknown[], Me = unknown>(
    func: (this: Me, ...args: A) => unknown,
    debounceMs: number,
    options?: DebounceOptions
) => ((this: Me, ...args: A) => void) & {
    schedule: () => void;
    cancel: () => void;
    flush: () => void;
};

function flow<R>(f: () => R): () => R;
function flow<A extends any[], R>(f1: (...args: A) => R): (...args: A) => R;
function flow<A extends any[], R1, R2>(f1: (...args: A) => R1, f2: (a: R1) => R2): (...args: A) => R2;
function flow<A extends any[], R1, R2, R3>(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3): (...args: A) => R3;
function flow<A extends any[], R1, R2, R3, R4>(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4): (...args: A) => R4;
function flow<A extends any[], R1, R2, R3, R4, R5>(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5): (...args: A) => R5;
function flow(...funcs: Array<(...args: any[]) => any>): (...args: any[]) => any;

function flowLast<R>(f: () => R): () => R;
function flowLast<A extends any[], R>(f1: (...args: A) => R): (...args: A) => R;
function flowLast<A extends any[], R1, R2>(f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R2;
function flowLast<A extends any[], R1, R2, R3>(f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R3;
function flowLast<A extends any[], R1, R2, R3, R4>(f4: (a: R3) => R4, f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R4;
function flowLast<A extends any[], R1, R2, R3, R4, R5>(f5: (a: R4) => R5, f4: (a: R3) => R4, f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R5;
function flowLast(...funcs: Array<(...args: any[]) => any>): (...args: any[]) => any;

const identity: <const T>(x: T) => T;

const negate: <A extends unknown[]>(func: (...args: A) => unknown) => (...args: A) => unknown;

const noop: () => void;

function once<F extends () => any>(func: F): F;

function once<F extends (...args: any[]) => void>(func: F): F;

const rest: <F extends (...args: any[]) => any>(func: F, startIndex?: number) => (...args: any[]) => ReturnType<F>;

const spread: <F extends (...args: any[]) => any>(func: F) => (argsArr: Parameters<F>) => ReturnType<F>;

interface ThrottleOptions {
    signal?: AbortSignal;
    edges?: Array<'leading' | 'trailing'>;
}
const throttle: <A extends unknown[], Me = unknown>(
    func: (this: Me, ...args: A) => unknown,
    throttleMs: number,
    options?: ThrottleOptions
) => ((this: Me, ...args: A) => void) & {
    cancel: () => void;
    flush: () => void;
};

const unary: <F extends (...args: any[]) => any>(func: F) => (...args: any[]) => ReturnType<F>;

const hook: <Args extends unknown[], R, Ctx = unknown>(
    fn: (this: Ctx, ...args: Args) => R,
    pre?: null | ((this: Ctx, args: Args) => Args | undefined | null | void),
    post?: (this: Ctx, res: R, args: Args, rawArgs?: Args) => R
) => (this: Ctx, ...args: Args) => R;

const clamp: (value: number, minimum: number, maximum?: number) => number;

function inRange(value: number, maximum: number): boolean;

function inRange(value: number, minimum: number, maximum: number): boolean;

const mean: (nums: readonly number[]) => number;

const meanBy: <T>(items: readonly T[], getValue: (element: T) => number) => number;

const median: (nums: readonly number[]) => number;

const medianBy: <T>(items: readonly T[], getValue: (element: T) => number) => number;

const gt: <T>(a: T, b: T) => boolean;

const gte: <T>(a: T, b: T) => boolean;

const lt: <T>(a: T, b: T) => boolean;

const lte: <T>(a: T, b: T) => boolean;

const neq: (a: unknown, b: unknown) => boolean;

const random: (minimum?: number, maximum?: number) => number;

function randomInt(maximum: number): number;
function randomInt(minimum: number, maximum: number): number;

function range(end: number): number[];
function range(start: number, end: number): number[];
function range(start: number, end: number, step: number): number[];

function rangeRight(end: number): number[];
function rangeRight(start: number, end: number): number[];
function rangeRight(start: number, end: number, step: number): number[];

const floor: (value: number, precision?: number) => number;

const ceil: (value: number, precision?: number) => number;

const round: (value: number, precision?: number) => number;

const sum: (nums: readonly number[]) => number;

const sumBy: <T>(items: readonly T[], getValue: (element: T) => number) => number;

const trunc: (value: number, precision?: number) => number;

const sign: (x: number) => number;

const defaults: <const O extends Record<PropertyKey, unknown>>(target: Partial<O>, ...sources: readonly Partial<O>[]) => Partial<O>;

const defaultTo: <T, V>(value: T, defaultValue: V) => T | V;

const entries: (<O extends Record<string, unknown> | ArrayLike<unknown>, K extends keyof O & string = keyof O & string, V extends O[K] = O[K]>(obj: O) => [K, V][]) | {
    <T>(o: {
        [s: string]: T;

    } | ArrayLike<T>): [string, T][];

    (o: {}): [string, any][];

};

const forIn: <V, K extends PropertyKey>(obj: { [k in K]: V; }, iterate: (val: V, key: K, obj: { [k in K]: V; }) => unknown) => { [k in K]: V; };

const forOwn: <V, K extends PropertyKey>(obj: { [k in K]: V; }, iterate: (val: V, key: K, obj: { [k in K]: V; }) => unknown) => { [k in K]: V; };

const findKey: <T extends object, K extends string & (keyof T) = string & keyof T>(obj: T,
    predicate: (value: T[K], key: K, obj: T) => boolean) => K | undefined;

const has: <const K extends PropertyKey>(target: object | null | undefined, propKey: K) => target is { [k in K]: unknown; };

const hasOwn: (obj: unknown, key: unknown) => boolean;

const invert: <const O extends Record<PropertyKey, PropertyKey>>(obj: O) => Inverted<O>;

const keys: <T>(x: T | null | undefined) => string[];

const omit: <T extends Record<string, any>, K extends keyof T>(obj: T, keys: readonly K[]) => Omit<T, K>;

const omitBy: <T extends Record<string, any>>(obj: T, shouldOmit: (value: T[keyof T], key: keyof T) => boolean) => Partial<T>;

const pick: <T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: readonly K[]) => Pick<T, K>;

const pickBy: <T extends Record<string, any>>(obj: T, shouldPick: (value: T[keyof T], key: keyof T) => boolean) => Partial<T>;

type Prop = {
    (obj: null | undefined, name: string | number | symbol): undefined;

    <const O extends object, const K extends string | number | symbol>(obj: O, name: K): O[K & keyof O];

};

const prop: Prop;

type GetValue<O, K> = K extends keyof O ? O[K] : undefined;

type AddValue<A extends readonly unknown[], T> = number extends A['length'] ? (A[number] | T)[] : [...A, T];

type GetValues<O, PS extends readonly unknown[], Res extends readonly unknown[] = []> = number extends PS['length'] ? GetValue<O, PS[number]>[] : PS extends readonly [] ? Res : PS extends readonly [infer K] ? AddValue<Res, GetValue<O, K>> : PS extends readonly [infer P, ...infer Rest] ? GetValues<O, Rest, AddValue<Res, GetValue<O, P>>> : AddValue<Res, GetValue<O, PS[number]>>;

const props: <const O extends Record<K, V>, const A extends readonly K[] | readonly [K], const V, K extends PropertyKey = PropertyKey>(obj: Partial<O>, names: A) => GetValues<O, A>;

const shake: <T extends object>(obj: T, test?: (value: unknown, key: keyof T, obj: T) => boolean) => Partial<T>;

const values: {
    <T>(o: {
        [s: string]: T;

    } | ArrayLike<T>): T[];

    (o: {}): any[];

};

const camelCase: (str: string) => string;

const capitalize: <T extends string>(str: T) => Capitalize<T>;

type Capitalize<T extends string> = T extends `${infer F}${infer R}` ? `${Uppercase<F>}${Lowercase<R>}` : T;

const constCase: (str: string) => string;

const endsWith: (str: string, suffix: string, endPosition?: number | boolean, ignoreCase?: boolean) => boolean;

const htmlEncode: (str: string) => string;

type DataValue = string | number | boolean | bigint | null | undefined;

type DataObject = Readonly<Record<string, DataValue>>;

type DataArray = readonly DataValue[];

function format(tpl: string, data: DataObject): string;

function format(tpl: string, data: DataArray): string;

function format(tpl: string, ...values: DataValue[]): string;

function format(tpl: string, ...values: [DataObject | DataArray | DataValue, ...DataValue[]]): string;

const dashCase: (str: string) => string;

const dotCase: (str: string) => string;

const lowerFirst: (str: string) => string;

const pad: (str: string, length: number, chars?: string) => string;

const padStart: (input: string, length: number, chars?: string) => string;

const padEnd: (input: string, length: number, chars?: string) => string;

const pascalCase: (str: string) => string;

const pathCase: (str: string) => string;

const snakeCase: (str: string) => string;

const startCase: (str: string) => string;

const swapCase: (input: string) => string;

const trim: (str: string, chars?: string | string[]) => string;

const trimEnd: (str: string, chars?: string | string[]) => string;

const trimStart: (str: string, chars?: string | string[]) => string;

const toLower: (str: string) => string;

const toUpper: (str: string) => string;

const htmlDecode: (str: string) => string;

const upperFirst: (str: string) => string;

const toFinite: (value?: unknown) => number;

const toInteger: (value?: unknown) => number;

const toLength: (value?: unknown) => number;

function toNumber(value?: unknown): number;

const toSafeInteger: (value?: unknown) => number;

function parse(queryStr: string): Record<string, string>;

function stringify(queryObj: Record<string, string | number | boolean>, prefix?: string | boolean): string;

interface MemoizeCache<K, V> {
    set(key: K, value: V): void;

    get(key: K): V | undefined;

    has(key: K): boolean;

    delete(key: K): boolean | void;

    clear(): void;

    size: number;

}
type MemoizeOptions<A extends unknown[], R, K, Me = unknown> = {
    cache?: MemoizeCache<K, R>;

    getKey?: (this: Me, ...args: A) => K;

};

type Memoized<A extends unknown[], R, K = string, Me = unknown> = {
    (this: Me, ...args: A): R;

    cache: MemoizeCache<K, R>;

};

const memoize: <A extends unknown[], R, K = string, Me = unknown>(fn: (this: Me, ...args: A) => R, options?: MemoizeOptions<A, R, K, Me>) => Memoized<A, R, K, Me>;

const defer: <A extends unknown[]>(func: (...args: A) => unknown, ...args: A) => ReturnType<typeof setTimeout>;

interface RetryOptions {
    multiplier?: number;

    maxTimeout?: number;

    maxAttempts?: number;

    minTimeout?: number;

    jitter?: number;

}
function retry<T>(fn: (() => Promise<T>) | (() => T), options?: RetryOptions): Promise<T>;

const sleep: (ms: number) => Promise<undefined>;

function timeout(ms: number): Promise<never>;

const withTimeout: <T>(run: () => Promise<T>, ms: number) => Promise<T>;

interface AssertTruthy {
    <T = true>(condition: T, errMsg?: string | Error): asserts condition is Truthy<T>;

}
const assertTruthy: AssertTruthy;