nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.
90 lines • 4.57 kB
TypeScript
import type { FlattenPartial } from '../types/index';
import type { FlattenLeafValue, FlattenValue, GenericObject, MergeAll, Objects, Expand } from './types';
/**
* Deeply merges two or more objects.
* Objects are merged recursively. Later values override earlier ones unless both are plain objects.
*
* @param objects - List of objects to be merged.
* @returns A new object with deeply merged properties from all input objects.
*
* @example
* const obj1 = { a: 1, b: { x: 10 } };
* const obj2 = { b: { y: 20 }, c: 3 };
* const merged = mergeObjects(obj1, obj2);
* // merged = { a: 1, b: { x: 10, y: 20 }, c: 3 }
*
* @example
* mergeObjects(
* { a: 1, b: 2 },
* { p: { c: 3 }, d: 4 },
* { p: { e: 5 }, f: 6 }
* );
* // => { a: 1, b: 2, p: { c: 3, e: 5 }, d: 4, f: 6 }
*/
export declare const mergeObjects: <T extends Objects>(...objects: T) => Expand<MergeAll<T>>;
/**
* * Deeply merge objects and flatten nested objects.
* * Useful for flattening a single object or merging multiple objects with duplicate key(s).
* * If keys are duplicated, the last object's value will be used.
*
* @param objects Objects to merge.
* @returns Merged object with flattened structure.
*/
export declare const mergeAndFlattenObjects: <T extends Objects>(...objects: T) => Expand<FlattenValue<MergeAll<T>>>;
/**
* * Flattens a nested object into key-value format.
*
* @param object - The `object` to flatten.
* @returns A `flattened object` in key-value format.
*/
export declare const flattenObjectKeyValue: <T extends GenericObject>(object: T) => Expand<FlattenLeafValue<MergeAll<[T]>>>;
/**
* * Flattens a nested object into a dot notation format.
*
* @param object - The `object` to flatten.
* @returns A `flattened object` with dot notation keys.
*/
export declare const flattenObjectDotNotation: <T extends GenericObject>(object: T) => Expand<FlattenValue<MergeAll<[T]>>>;
/**
* * Extracts only the fields that have changed between the original and updated object.
*
* @param baseObject The original object to compare against.
* @param updatedObject The modified object containing potential updates.
* @returns A new object containing only the changed fields.
*/
export declare const extractUpdatedFields: <T extends GenericObject>(baseObject: T, updatedObject: FlattenPartial<T>) => FlattenPartial<T>;
/**
* * Extracts only new fields that exist in updatedObject but not in baseObject.
*
* @param baseObject The original object to compare against.
* @param updatedObject The modified object containing potential new fields.
* @returns A new object containing only the new fields.
*/
export declare const extractNewFields: <T extends GenericObject, U extends GenericObject>(baseObject: T, updatedObject: FlattenPartial<T> & FlattenPartial<U>) => FlattenPartial<U>;
/**
* * Extracts changed fields from the updated object while also identifying newly added keys.
*
* @param baseObject The original object to compare against.
* @param updatedObject The modified object containing potential updates.
* @returns An object containing modified fields and new fields separately.
*/
export declare const extractUpdatedAndNewFields: <T extends GenericObject, U extends GenericObject>(baseObject: T, updatedObject: FlattenPartial<T> & FlattenPartial<U>) => FlattenPartial<T> & FlattenPartial<U>;
/**
* * Safely parses a JSON string into an object.
* * Optionally converts stringified primitive values inside the object (e.g., `"0"` → `0`, `"true"` → `true`, `"null"` → `null`).
*
* @param value - The JSON string to parse.
* @param parsePrimitives - Whether to convert stringified primitives into real values (default: `true`).
* @returns A parsed object with primitive conversions, or an empty object on failure or if the root is not a valid object.
* - Returns `{}` if parsing fails, such as when the input is malformed or invalid JSON or passing single quoted string.
*
* - **N.B.** This function will return an empty object if the JSON string is invalid or if the root element is not an object.
*
* - *Unlike `parseJSON`, which returns any valid JSON structure (including arrays, strings, numbers, etc.),
* this function strictly ensures that the result is an object and optionally transforms stringified primitives.*
*
* @see parseJSON - For parsing generic JSON values (arrays, numbers, etc.) with optional primitive transformation.
*
*/
export declare const parseJsonToObject: <T extends GenericObject = GenericObject>(value: string, parsePrimitives?: boolean) => T;
//# sourceMappingURL=objectify.d.ts.map