nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.
85 lines (84 loc) • 4.31 kB
TypeScript
import type { ConvertedObject, ConvertObjectOptions, GenericObject, NumericDotKey } from './types';
/**
* * Converts the values of specified keys in an object to `"string"` or `"number"`.
* * Supports nested objects using dot-notation keys.
*
* @param data The object to convert.
* @param options Options object specifying the conversion mapping.
* - `keys`: The keys in the objects to be converted (dot-notation supported).
* - `convertTo`: The target type, either `"string"` or `"number"`.
* @returns The modified object with the converted values as `"string"` or `"number"`.
*/
export declare function convertObjectValues<T extends GenericObject, Key extends NumericDotKey<T>, C extends 'string' | 'number'>(data: T, options: ConvertObjectOptions<T, Key, C>): ConvertedObject<T, Key, C>;
/**
* * Converts the values of specified keys in an array of objects to `"string"` or `"number"`.
* * Supports nested objects using dot-notation keys.
*
* @param data The array of objects to convert.
* @param options Options object specifying the conversion mapping.
* - `keys`: The keys in the objects to be converted (dot-notation supported).
* - `convertTo`: The target type, either `"string"` or `"number"`.
* @returns The modified array of objects with the converted values as `"string"` or `"number"`.
*/
export declare function convertObjectValues<T extends GenericObject, Key extends NumericDotKey<T>, C extends 'string' | 'number'>(data: Array<T>, options: ConvertObjectOptions<T, Key, C>): Array<ConvertedObject<T, Key, C>>;
/**
* * Pick specific fields from an object and create a new object with specified fields.
*
* @description This function creates a new object containing only the specified fields from the source object.
* It is useful for creating a new object with a subset of properties from an existing object.
*
* @param T The type of the source object.
* @param U The type of the keys to pick from the source object.
*
* @param source The source object from which to pick fields.
* @param keys The keys of the fields to pick from the source object.
*
* @returns An object containing only the picked fields.
*/
export declare function pickFields<T extends GenericObject, U extends keyof T>(source: T, keys: U[]): {
[K in U]: T[K];
};
/**
* * Create a new object by removing specific keys from the source object.
*
* @param source - The original (source) object from which to delete fields.
* @param keys - An array of keys (fields) to remove from the object.
*
* @returns A new object without the specified keys.
*
* @example
* deleteFields({ a: 1, b: 2, c: 3 }, ['b'])
* // => { a: 1, c: 3 }
*
* @notes
* - Does not mutate the original object.
* - Useful for excluding sensitive or unwanted fields.
*/
export declare function deleteFields<T extends GenericObject, U extends keyof T>(source: T, keys: readonly U[]): {
[K in Exclude<keyof T, U>]: T[K];
};
/**
* * Pick specific fields from an object based on a given condition.
*
* @description This function creates a new object containing only the fields that satisfy the given condition.
* The condition can be based on the field's value or type, depending on the implementation.
*
* @param T The type of the source object.
*
* @param source The source object from which to pick fields.
* @param condition A function that takes the key and value of a property and returns a boolean indicating whether the property should be picked.
*
* @returns An object containing only the fields that satisfy the condition.
*/
export declare function pickObjectFieldsByCondition<T extends GenericObject>(source: T, condition: (key: keyof T, value: T[keyof T]) => boolean): Partial<T>;
/**
* * Remap fields from one object to another.
* @description This function creates a new object with fields remapped from the source object to the target object based on the provided field map.
*
* @param source The source object from which to remap fields.
* @param fieldMap An object that maps target keys to source keys.
* @returns An object with fields remapped according to the field map.
*/
export declare function remapFields<Source extends GenericObject, Target extends Record<string, keyof Source>>(source: Source, fieldMap: Target): {
[K in keyof Target]: Source[Target[K]];
};