@technobuddha/library
Version:
A large library of useful functions
21 lines (20 loc) • 948 B
TypeScript
import { type TBJsonValue } from './json.ts';
/**
* Recursively sorts the keys of an object in lexicographical order.
*
* If the input is a primitive value or an array, it is returned as-is.
* For objects, all keys are sorted, and the function is applied recursively to all values.
* @typeParam T - The type of the input value, extending JsonValue.
* @param object - The object or value whose keys should be sorted.
* @returns A new object with keys sorted, or the original value if it is a primitive or array.
* @group Object
* @category Operations
* @example
* ```typescript
* sortKeys(\{ b: 2, a: 1 \}); // returns \{ a: 1, b: 2 \}
* sortKeys(\{ z: 1, y: \{ b: 2, a: 1 \} \}); // returns \{ y: \{ a: 1, b: 2 \}, z: 1 \}
* sortKeys([\{ b: 2, a: 1 \}, \{ d: 4, c: 3 \}]); // returns [\{ a: 1, b: 2 \}, \{ c: 3, d: 4 \}]
* sortKeys(42); // returns 42
* ```
*/
export declare function sortKeys<T extends TBJsonValue>(object: T): T;