sort-by-property
Version:
Type-safe array sorting method with support for deeply nested properties and Typescript autocompletion.
13 lines • 1.08 kB
TypeScript
type SortFunctionReturn<T> = (a: T, b: T) => number;
interface SortOptions {
locale?: Parameters<typeof String.prototype.localeCompare>[1];
}
export type SortByDirection = 'asc' | 'desc';
export type SupportedTypes = number | number[] | string | string[] | boolean | boolean[] | Date | Date[] | bigint | bigint[] | symbol | symbol[] | null | undefined;
export declare function sortBy<T>(direction: SortByDirection, options?: SortOptions): SortFunctionReturn<T>;
export type PropertyPath<T> = {
[K in keyof T & string]: T[K] extends Record<string, unknown> ? PropertyPath<T[K]> extends infer S ? `${K}.${S & string}` : never : T[K] extends SupportedTypes ? K : T[K] extends unknown[] ? PropertyPath<T[K][number]> extends infer S ? `${K}.${S & string}` : never : PropertyPath<T[K]> extends infer S ? `${K}.${S & string}` : never;
}[keyof T & string];
export declare function sortByProperty<T extends Record<string, any>>(propertyPath: PropertyPath<T>, direction: SortByDirection, options?: SortOptions): SortFunctionReturn<T>;
export {};
//# sourceMappingURL=index.d.ts.map