UNPKG

@pivanov/utils

Version:

A focused collection of TypeScript utilities for modern web development

119 lines (118 loc) 4.06 kB
import type { TDict } from "../types"; /** * Creates a new object with the specified keys removed. * * @example * ```ts * omit({ name: 'John', age: 30 }, ['age']); // { name: 'John' } * ``` */ export declare const omit: <T extends TDict, K extends keyof T>(object: T, keys: K[]) => Omit<T, K>; /** * Creates a new object with only the specified keys. * * @example * ```ts * pick({ name: 'John', age: 30 }, ['name']); // { name: 'John' } * ``` */ export declare const pick: <T extends TDict, K extends keyof T>(object: T, keys: K[]) => Pick<T, K>; /** * Returns a new object keeping entries where the predicate returns true. * * @example * ```ts * pickBy({ a: 1, b: 2, c: 3 }, (v) => v > 1); // { b: 2, c: 3 } * ``` */ export declare const pickBy: <T extends TDict>(object: T, predicate: (value: T[keyof T], key: keyof T) => boolean) => Partial<T>; /** * Returns a new object dropping entries where the predicate returns true. * * @example * ```ts * omitBy({ a: 1, b: null, c: 3 }, (v) => v === null); // { a: 1, c: 3 } * ``` */ export declare const omitBy: <T extends TDict>(object: T, predicate: (value: T[keyof T], key: keyof T) => boolean) => Partial<T>; /** * Returns a new object with values mapped via the transform function. * * @example * ```ts * mapValues({ a: 1, b: 2 }, (v) => v * 2); // { a: 2, b: 4 } * ``` */ export declare const mapValues: <T extends TDict, R>(object: T, mapper: (value: T[keyof T], key: keyof T) => R) => Record<keyof T, R>; /** * Returns a new object with keys mapped via the transform function. * * @example * ```ts * mapKeys({ a: 1, b: 2 }, (_, k) => k.toUpperCase()); // { A: 1, B: 2 } * ``` */ export declare const mapKeys: <T extends TDict>(object: T, mapper: (value: T[keyof T], key: keyof T) => string) => Record<string, T[keyof T]>; /** * Groups items by the key returned by the iteratee. * * @example * ```ts * groupBy(['apple', 'banana', 'cherry'], (s) => s[0]); * // { a: ['apple'], b: ['banana'], c: ['cherry'] } * ``` */ export declare const groupBy: <T, K extends string | number>(items: readonly T[], iteratee: (item: T, index: number) => K) => Record<K, T[]>; /** * Swaps keys with values. Values must be valid object keys. * * @example * ```ts * invert({ a: 'x', b: 'y' }); // { x: 'a', y: 'b' } * ``` */ export declare const invert: <K extends string, V extends string | number | symbol>(object: Record<K, V>) => Record<V, K>; /** * Typed `Object.hasOwn`. Narrows the key into the object's own keys. * * @example * ```ts * if (hasOwn(obj, 'name')) obj.name; // narrowed * ``` */ export declare const hasOwn: <T extends object, K extends PropertyKey>(object: T, key: K) => object is T & Record<K, unknown>; /** * Typed `Object.keys`. Returns `(keyof T)[]` instead of `string[]`. * * Note: like `Object.keys`, the runtime keys are just the own enumerable * string keys, so this typing can be unsound if the object has extra runtime * properties not in its compile-time type. */ export declare const keysOf: <T extends object>(object: T) => (keyof T)[]; /** * Typed `Object.entries`. Returns `[keyof T, T[keyof T]][]`. */ export declare const entriesOf: <T extends object>(object: T) => [keyof T, T[keyof T]][]; /** * Typed `Object.fromEntries` for tuple arrays with literal key types. */ export declare const fromEntries: <K extends PropertyKey, V>(entries: readonly (readonly [K, V])[]) => Record<K, V>; /** * Shallow-merges multiple objects into a new object. Does not mutate inputs. * * @example * ```ts * merge({ a: 1 }, { b: 2 }, { c: 3 }); // { a: 1, b: 2, c: 3 } * ``` */ export declare const merge: <T extends object>(target: T, ...sources: Partial<T>[]) => T; /** * Recursively merges multiple objects into a new object. Does not mutate * inputs. Nested plain objects are merged; arrays and other values replace. * * @example * ```ts * deepMerge({ a: { b: 1 } }, { a: { c: 2 } }); // { a: { b: 1, c: 2 } } * ``` */ export declare const deepMerge: <T extends object>(target: T, ...sources: Partial<T>[]) => T;