nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.
87 lines (86 loc) • 4.25 kB
TypeScript
import type { GenericObject, NestedPrimitiveKey } from '../object/types';
import type { Maybe } from '../types/index';
import type { FieldValue, OptionsConfig } from './types';
/**
* * Converts an array of objects into a formatted array of options.
*
* @param data - An array of objects to convert into options.
* @param config - The configuration object to specify the keys for the `value` (firstFieldName) and `label` (secondFieldName) fields and rename as needed.
* @returns An array of options, where each option has `value` and `label` fields as default or as specified by user in the config options.
*/
export declare function createOptionsArray<T extends GenericObject, K1 extends string = 'value', K2 extends string = 'label', V extends boolean = false>(data: T[], config: OptionsConfig<T, K1, K2, V>): Array<{
[P in K1 | K2]: FieldValue<P, T, K1, K2, V>;
}>;
/**
* * Removes duplicate values from an array, supporting deep comparison for objects and arrays.
*
* @param array - The array from which duplicates need to be removed.
* @returns A new array with duplicates removed.
*/
export declare function removeDuplicatesFromArray<T>(array: T[]): T[];
/**
* * Finds duplicate values in an array, runs deep comparison for objects and arrays.
*
* @param array - The array in which to find duplicates.
* @returns An array containing all duplicate entries (each one only once).
*/
export declare function getDuplicates<T>(array: T[]): T[];
/**
* * Finds elements missing from one array compared to another using deep comparison.
*
* @param options - Configuration to specify which array to compare and direction of check.
* @returns An array of missing elements based on the comparison direction.
*/
/**
* * Finds elements missing from one array compared to another using deep comparison.
*
* @param array1 The first array to compare.
* @param array2 The second array to compare.
* @param missingFrom Which direction to compare for missing values:.
* - `'from-first'` → values in `array1` missing in `array2`.
* - `'from-second'` → values in `array2` missing in `array1`.
* @returns An array of missing elements based on the comparison direction.
*/
export declare function findMissingElements<T, U>(array1: T[], array2: U[], missingFrom: 'from-first' | 'from-second'): (T | U)[];
/**
* * Splits an array into chunks of a given size.
*
* @param arr The array to split.
* @param chunkSize The size of each chunk.
* @returns An array of chunked arrays.
*/
export declare function splitArray<T>(arr: T[], chunkSize: number): Array<T[]>;
/**
* * Group an array of objects by a specified key, returning only arrays of grouped objects.
*
* @param source - The source array of objects to group.
* @param property - The property to group the array by. Property can be a string, number, boolean, undefined or null. Supports nested dot notation.
*
* @returns An array of grouped arrays. Each sub-array contains objects that share the same value for the specified property.
*
* @example
* splitArrayByProperty([{ type: 'a' }, { type: 'b' }, { type: 'a' }, { type: undefined }], 'type')
* // => [ [{ type: 'a' }, { type: 'a' }], [{ type: 'b' }], [{ type: undefined }] ]
*
* @notes
* - Returns an empty array if the input is invalid or empty.
* - Groups objects even when the group key is `undefined` or `null` (object with `null` & `undefined` property-values are grouped together).
*/
export declare function splitArrayByProperty<T extends GenericObject, P extends NestedPrimitiveKey<T>>(source: Maybe<T[]>, property: P): Array<T[]>;
/**
* * Rotates an array left or right by a given number of steps.
*
* @param arr The array to rotate.
* @param steps The number of positions to rotate (positive: right, negative: left).
* @returns The rotated array.
*/
export declare function rotateArray<T>(arr: T[], steps: number): T[];
/**
* * Moves an element within an array from one index to another.
*
* @param arr The array to modify.
* @param fromIndex The index of the element to move.
* @param toIndex The new index for the element.
* @returns A new array with the element moved.
*/
export declare function moveArrayElement<T>(arr: T[], fromIndex: number, toIndex: number): T[];