dbl-utils
Version:
Utilities for dbl, adba and others projects
103 lines (102 loc) • 3.36 kB
TypeScript
/**
* Type definition for the options parameter
*/
type FlatCopyOptions = {
delimiter?: string;
ommit?: string[];
ommitArrays?: boolean;
prefix?: string;
ommitFn?: (key: string, value: any, newIndex: string) => boolean;
};
/**
* Flattens an object into a single-level object with delimited keys.
*
*
* @param object - The object to flatten
* @param options - Configuration options for flattening
* @returns A single-level object with flattened keys
* @throws Will throw an error if the input is not an object
*
* @example
* ```ts
* const result = flatten({ a: { b: 1 } });
* // result => { 'a.b': 1 }
* ```
*/
export declare function flatten(object: object, options?: FlatCopyOptions): Record<string, any>;
/**
* Reconstructs a nested object from a flattened one using a delimiter.
* Numeric keys are automatically treated as array indices so arrays
* are recreated correctly.
*
* @param object - The flattened object.
* @param delimiter - The string delimiter used to split keys.
* @returns The nested (unflattened) object.
*
* @example
* ```ts
* const obj = { 'a.0': 1, 'a.1': 2 };
* unflatten(obj);
* // => { a: [1, 2] }
* ```
*/
export declare function unflatten(object: Record<string, any>, delimiter?: string): Record<string, any>;
/**
* Converts a string into its original type (string, number, boolean).
*/
export declare function formatAs(value: string, type: string): string | number | boolean;
/**
* Adds extra formatter functions to the internal registry.
*/
export declare const addFormatters: (extraFormatters: Record<string, (v: string) => any>) => boolean;
/**
* Serializes an object or array of objects into a flat list of field entries.
* You can customize the process using `before` (pre-processing per item)
* and `after` (post-processing per field).
*
* @example
* ```ts
* const data = { a: 1, b: { c: 2 } };
* const entries = await serialize(data);
* // entries[0] => { path: 'a', value: '1', type: 'number', uuid: '...' }
* ```
*/
export declare const serialize: (data: any, opts?: {
before?: (element: any, index: number) => Promise<Record<string, any> | null | false | undefined>;
after?: (path: string, value: string | number | boolean, metaInfo: Record<string, any>) => Promise<string | number | boolean | null | undefined>;
metaIdentifier?: string;
groupKey?: string;
}) => Promise<(false | {
[x: string]: any;
path: string;
value: string;
type: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
})[]>;
/**
* Deserializes a flat list of entries into full objects.
* Groups entries by a defined key (e.g. uuid or content_uuid) if present.
* Optionally applies a `before` hook to each entry before processing.
*
* @example
* ```ts
* const objects = await deserialize(entries);
* // objects => [{ a: 1, b: { c: 2 } }]
* ```
*/
export declare const deserialize: (serialized: Array<{
path: string;
value: string;
type: string;
[meta: string]: any;
}>, opts?: {
before?: (entry: {
path: string;
value: string;
type: string;
[meta: string]: any;
}) => Promise<Record<string, any> | null | false | undefined>;
metaIdentifier?: string;
groupKey?: string;
metaObject?: boolean;
}) => Promise<Record<string, any>[]>;
export {};