UNPKG

dbl-utils

Version:

Utilities for dbl, adba and others projects

70 lines (69 loc) 2.19 kB
type MutationFunction = (key: string, value: any, data: any) => any; interface MergeOptions { mutation: MutationFunction; ommit?: string[]; data?: any; } interface ConfigOptions { fix?: (target: any, source: any) => any; } interface TransformOptions { beforeFunc?: (context: Context) => any; duringFunc?: (context: Context) => any; afterFunc?: (context: Context) => any; nonObjectFunc?: (context: Context) => any; filter?: string | string[] | ((context: Context) => boolean); key?: string; root?: any; } interface Context { input: any; key: string | null; parentObj: any; parentKey: string | null; path: string; } /** * Execute a mutation function on the object and merge results. * @param target - The target object to be merged with mutation. * @param options - Options for mutation, ommit keys and additional data. * @param parentKey - Internal tracking key for recursion. * @returns The mutated and merged target object. * * @example * ```ts * await mergeWithMutation({ a: { b: 1 } }, { mutation: () => ({ c: 2 }) }); * // => { a: { b: 1, c: 2 } } * ``` */ export declare function mergeWithMutation(target: any, { mutation, ommit, data }: MergeOptions, parentKey?: string): Promise<any>; /** * Deep merge multiple objects. * @param target - The target object where other objects will be merged. * @param sources - Other objects to be merged into the target. * @returns The merged object. * * @example * ```ts * deepMerge({}, { a: 1 }, { b: 2 }); // { a: 1, b: 2 } * ``` */ export declare function deepMerge(target: any, ...sources: any[]): any; export declare namespace deepMerge { var setConfig: (config: ConfigOptions) => void; } /** * Transforms and flattens a JSON object. * * @param json - The JSON object to be transformed. * @param options - Options for the mutation functions. * @returns An array with the modified object and the flattened object. * * @example * ```ts * transformJson({ a: { b: 1 } }, { filter: 'a' }); * // => [{ a: { b: 1 } }, { a: { b: 1 } }] * ``` */ export declare function transformJson(json: any, options?: TransformOptions): [any, any]; export {};