Utilities shared across @1k packages
npm install @1k/shared``ts
const eq:
const kindOf:
const isArguments: (it?: unknown) => it is IArguments;
const isArray:
const isArrayBuffer: (value?: unknown) => value is ArrayBuffer;
const isArrayBufferView: (it: unknown) => it is ArrayBufferView;
const isArrayLike: (value: unknown) => value is ArrayLike
const isArrayLikeObject: (value: unknown) => value is ArrayLike
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 isError: (x: unknown) => x is Error;
const isEven: (x: unknown) => x is number;
type Falsy
const isFalsy:
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
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
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 isNull: (x: unknown) => x is null;
const isNumber: (v: unknown) => v is number;
const isNumeric: (n: unknown) => n is string | number;
const isObject:
function isObjectLike(value: unknown): value is object;
const isOdd: (x: unknown) => x is number;
const isPlainObject: (value: unknown) => value is Record
function isPositive
const isPresent:
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:
const isPromiseLike:
const isRegExp:
const isSafeInteger: (n: unknown) => n is number;
const isScalar: (it: unknown) => it is string | number | boolean;
const isSet:
const isString: (it: unknown) => it is string;
const isSymbol: (it: unknown) => it is symbol;
type Truthy
const isTruthy:
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
const isWeakSet: (value: unknown) => value is WeakSet
const isWellFormed: (str: string) => boolean;
const isWindow: (it: unknown) => it is Window;
const now: () => number;
const chunk:
const compact:
const countBy:
list: ArrayLike
const drop:
const dropLast:
const dropLastWhile:
list: readonly T[], canContinueDropping: (item: T, index: number, arr: readonly T[]) => boolean) => T[];
const dropWhile:
function fill
function fill
function fill
function first
function first
const groupBy:
list: ArrayLike
interface Partition {
(
list: ArrayLike, isTruthy: (item: U, i: number, list: ArrayLike) => item is T): [truthy: T[], falsy: Exclude[]];
list: ArrayLike
}
const partition: Partition;
const keyBy:
list: ArrayLike
function last
function last
function maxBy
function maxBy
function minBy
function minBy
const sample:
const shuffle:
const take:
const takeLast:
const takeLastWhile:
const takeWhile:
function contains
function every
list: T[],
predicate: (element: T, index: number, list: ArrayLike
): list is U[];
function every
list: readonly T[],
predicate: (element: T, index: number, list: ArrayLike
): list is readonly U[];
function every
list: ArrayLike
predicate: (element: T, index: number, list: ArrayLike
): boolean;
function filter
array: ArrayLike
f: (x: T, i: number, list: ArrayLike
startIndex?: number,
limit?: number ): U[];
function filter
list: ArrayLike
f: (x: T, i: number, list: ArrayLike
startIndex?: number,
limit?: number ): T[];
function find
array: readonly T[] | null | undefined,
predicate: (value: T, index: number,
list: ArrayLike
startIndex?: number,
limit?: number ): U | undefined;
function find
list: ArrayLike
predicate: (value: T, index: number, list: ArrayLike
startIndex?: number,
limit?: number ): T | undefined;
const findIndex:
list: ArrayLike
predicate: (v: T, i: number, a: ArrayLike
startIndex?: number,
limit?: number ) => number;
function findLast
array: readonly T[] | null | undefined,
predicate: (value: T, index: number, list: ArrayLike
startIndex?: number,
limit?: number ): U | undefined;
function findLast
list: ArrayLike
predicate: (v: T, i: number, list: ArrayLike
startIndex?: number,
limit?: number ): T | undefined;
const findLastIndex:
list: ArrayLike
predicate: (v: T, i: number, a: ArrayLike
startIndex?: number,
limit?: number ) => number;
function each
list: ArrayLike
iterate: (element: T, index: number, list: ArrayLike
startIndex?: number,
limit?: number): ArrayLike
function eachLast
list: ArrayLike
iterate: (element: T, index: number, list: ArrayLike
startIndex?: number,
limit?: number): ArrayLike
const includes:
list: readonly T[] | { includes(el: T, fromIndex?: number): boolean; },
el: T,
fromIndex?: number) => boolean;
const indexOf:
list: ArrayLike
value: T | string,
startIndex?: number) => number;
const lastIndexOf:
list: ArrayLike
function map
list: ArrayLike
function map
function map
list: ArrayLike
function reduce
function reduce
function reduce
function reject
list: ArrayLike
f: (x: T, i: number, list: ArrayLike
startIndex?: number,
limit?: number): T[];
function some
list: ArrayLike
predicate: (value: T, index: number, list: ArrayLike
): boolean;
const after: (
n: number,
func: (...args: A) => R
) => (...args: A) => R | undefined;
const ary: (
func: (this: This, ...args: A) => R, n: number
) => (this: This, ...args: A) => R;
const attempt: (
func: (...args: A) => R, ...args: A
) => R | Error;
const always:
const alwaysTrue: () => true;
const alwaysFalse: () => false;
const before: (
n: number,
func: (...args: A) => R
) => (...args: A) => R | undefined;
interface DebounceOptions {
signal?: AbortSignal;
edges?: Array<'leading' | 'trailing'>;
}
function flow
function flow(f1: (...args: A) => R): (...args: A) => R;
function flow(f1: (...args: A) => R1, f2: (a: R1) => R2): (...args: A) => R2;
function flow(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3): (...args: A) => R3;
function flow(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4): (...args: A) => R4;
function flow(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
function flowLast(f1: (...args: A) => R): (...args: A) => R;
function flowLast(f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R2;
function flowLast(f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R3;
function flowLast(f4: (a: R3) => R4, f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R4;
function flowLast(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 negate: (func: (...args: A) => unknown) => (...args: A) => unknown;
const noop: () => void;
function once
function once
const rest:
const spread:
interface ThrottleOptions {
signal?: AbortSignal;
edges?: Array<'leading' | 'trailing'>;
}
const throttle: (
func: (this: Me, ...args: A) => unknown,
throttleMs: number,
options?: ThrottleOptions
) => ((this: Me, ...args: A) => void) & {
cancel: () => void;
flush: () => void;
};
const unary:
const hook:
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:
const median: (nums: readonly number[]) => number;
const medianBy:
const gt:
const gte:
const lt:
const lte:
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:
const trunc: (value: number, precision?: number) => number;
const sign: (x: number) => number;
const defaults:
const defaultTo:
const entries: (
[s: string]: T;
} | ArrayLike
(o: {}): [string, any][];
};
const forIn:
const forOwn:
const findKey:
predicate: (value: T[K], key: K, obj: T) => boolean) => K | undefined;
const has:
const hasOwn: (obj: unknown, key: unknown) => boolean;
const invert:
const keys:
const omit:
const omitBy:
const pick:
const pickBy:
type Prop = {
(obj: null | undefined, name: string | number | symbol): undefined;
};
const prop: Prop;
type GetValue
type AddValue = number extends A['length'] ? (A[number] | T)[] : [...A, T];
type GetValues
const props:
const shake:
const values: {
[s: string]: T;
} | ArrayLike
(o: {}): any[];
};
const camelCase: (str: string) => string;
const capitalize:
type Capitalize ? ${Uppercase : 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
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
function stringify(queryObj: Record
interface MemoizeCache
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 = {
cache?: MemoizeCache
getKey?: (this: Me, ...args: A) => K;
};
type Memoized = {
(this: Me, ...args: A): R;
cache: MemoizeCache
};
const memoize: (fn: (this: Me, ...args: A) => R, options?: MemoizeOptions) => Memoized;
const defer: (func: (...args: A) => unknown, ...args: A) => ReturnType
interface RetryOptions {
multiplier?: number;
maxTimeout?: number;
maxAttempts?: number;
minTimeout?: number;
jitter?: number;
}
function retry
const sleep: (ms: number) => Promise
function timeout(ms: number): Promise
const withTimeout:
interface AssertTruthy {
}
const assertTruthy: AssertTruthy;
``