safers
Version:
Safely convert to the desired data and error-free refine values.
50 lines (49 loc) • 1.16 kB
TypeScript
/**
* Removes empty keys from an object.
*
* @remark
* - Empty keys are keys with null, undefined, empty string, empty array, or empty object values.
* - If `keysToKeep` is provided, it will not remove empty keys that match the provided keys.
*
* @param obj - The object to remove empty keys from.
* @param keysToKeep - The keys to keep even if they are empty (optional).
* @returns A new object with empty keys removed.
*
* @example
* ```ts
* const obj = {
* name: "John",
* age: 23,
* address: {
* street: "123 Main St.",
* city: "New York",
* state: "NY",
* zip: "",
* },
* };
*
* removeEmptyKeys(obj);
* // {
* // name: "John",
* // age: 23,
* // address: {
* // street: "123 Main St.",
* // city: "New York",
* // state: "NY",
* // },
* // }
*
* removeEmptyKeys(obj, ["address.zip"]);
* // {
* // name: "John",
* // age: 23,
* // address: {
* // street: "123 Main St.",
* // city: "New York",
* // state: "NY",
* // zip: "",
* // },
* // }
* ```
*/
export declare function removeEmptyKeys<T extends Record<string, any>>(obj: T, keysToKeep?: string[]): T;