xuxi
Version:
Dynamically utility for combining different types of values into a single value.
74 lines (73 loc) • 3.6 kB
TypeScript
/** A type alias representing an object with string keys and values of any type. */
export type ocxKey = {
[key: string]: any;
};
/** A generic type that extends `T` with `ocxKey`, allowing `T` to have arbitrary key-value pairs. */
export type ocxAcc<T> = T & ocxKey;
/**
* A flexible mapping type that can be an:
* - plain object (`ocxKey`)
* - array of objects (`ocxKey[]`)
* - recursive mapping (`ocxMap[]`)
* - primitive value (`string`, `number`, `null`, `boolean`, `undefined`)
* - function that takes an optional object (`ocxKey`) and returns an `ocxMap`. */
export type ocxMap = ocxKey | ocxKey[] | ocxMap[] | string | number | null | boolean | undefined | ((key?: ocxKey) => ocxMap);
/** An object that can be processed by `ocx`. */
export type ocxObj<T> = T | ocxMap | ocxAcc<T>;
/**
* Checks if a given value is a plain object (i.e., not an array or null).
* @param value - The value to check.
* @returns True if the value is a plain object, otherwise false.
*/
export declare function isPlainObject(value: unknown): value is ocxKey;
/**
* Merges multiple objects deeply, handling arrays and functions gracefully.
* @template T - The base object type.
* @param obj - One or more objects to merge.
* @returns The deeply merged object.
*/
declare function baseOcx<T extends ocxKey>(...obj: ocxObj<T>[]): ocxAcc<T>;
/**
* Merges multiple objects deeply, handling arrays and functions gracefully **without overwriting**.
* @template T - The base ocx type.
* @param obj - One or more objects to merge.
* @returns The deeply merged object **without overwriting** the value at the first key, only change the value if it does not exist.
*/
declare function preserveRoot<T extends ocxKey>(...obj: ocxObj<T>[]): ocxAcc<T>;
/**
* Recursively removes falsy values from an object, except those specified in `exclude`.
* @template T - The ocx type.
* @param obj - The object to clean.
* @param exclude - An array of values to be preserved even if they are falsy (default: `[]`).
* @param seen - To detect cyclic references (default: `new WeakSet<object>()`).
* @returns A new object without the falsy values.
* @example
* @see {@link https://ilkhoeri.github.io/xuxi/clean Docs}
*/
export declare function clean<T extends ocxKey>(obj: T, exclude?: unknown[], seen?: WeakSet<object>): T;
interface ocxFunction {
/**
* Merges multiple objects and removes falsy values by default.
* @template T - The base object type.
* @param obj - One or more objects to merge.
* @returns The deeply merged object with falsy values removed.
*/
<T extends ocxKey>(...obj: ocxObj<T>[]): ocxAcc<T>;
/** A version of `object` that performs deep merging **without** removing falsy values. */
raw: typeof baseOcx;
/** A version of `object` that performs a deep join **without overwriting** the value at the first key, only change the value if it does not exist. */
preserve: typeof preserveRoot;
}
/**
* Recursively merge objects with support for arrays, dynamic functions, and non falsy properties into a single object.
*
* Provides a chaining:
* - {@link baseOcx raw} method to **get falsy values** from the result.
* - {@link preserveRoot preserve} method to join **without overwriting** first value.
* @example
* @see {@link https://ilkhoeri.github.io/xuxi/?id=ocx Docs}
*/
declare const object: ocxFunction;
/** Recursively merge objects with support for arrays, dynamic functions, and non falsy properties into a single object. */
declare function ocx<T extends ocxKey>(...obj: ocxObj<T>[]): ocxAcc<T>;
export { ocx, object };