mergekit
Version:
Uniquely flexible and light-weight utility for cloning and deep (recursive) merging of JavaScript objects. Supports descriptor values, accessor functions, and custom prototypes. Provides advanced options for customizing the clone/merge process.
77 lines (74 loc) • 2.26 kB
text/typescript
interface MergekitOptions {
onlyKeys: string[];
skipKeys: string[];
onlyCommonKeys: boolean;
onlyUniversalKeys: boolean;
skipCommonKeys: boolean;
skipUniversalKeys: boolean;
onlyObjectWithKeyValues: {
key: string;
value: any;
}[];
invokeGetters: boolean;
skipSetters: boolean;
appendArrays: boolean;
prependArrays: boolean;
dedupArrays: boolean;
sortArrays: boolean | ((a: any, b: any) => number);
hoistEnumerable: boolean;
hoistProto: boolean;
skipProto: boolean;
filter?: (callbackData: CallbackData) => boolean | void;
beforeEach?: (callbackData: CallbackData) => any;
afterEach?: (callbackData: AfterEachCallbackData) => any;
onCircular: (callbackData: CallbackData) => any;
}
interface CallbackData {
depth: number;
key: string;
srcObj: object;
srcVal: any;
targetObj: object;
targetVal: any;
}
interface AfterEachCallbackData {
depth: number;
key: string;
mergeVal: any;
srcObj: object;
targetObj: object;
}
/**
* Merges multiple objects into one, with various options for customization.
*
* @param {object[] | object} objects - The objects to be merged. Can be a single object or an array of objects.
* @param {Partial<MergekitOptions>} [options=defaults] - Optional settings to customize the merge behavior.
* @returns {object} - The merged object.
*
* @example
* // Basic usage
* const obj1 = { a: 1, b: 2 };
* const obj2 = { b: 3, c: 4 };
* const result = mergekit([obj1, obj2]);
* // result: { a: 1, b: 3, c: 4 }
*
* @example
* // Using options
* const obj1 = { a: 1, b: 2 };
* const obj2 = { b: 3, c: 4 };
* const options = { onlyCommonKeys: true };
* const result = mergekit([obj1, obj2], options);
* // result: { b: 3 }
*
* @example
* // Merging with custom prototype properties
* function CustomProto() {}
* CustomProto.prototype.customMethod = function() { return 'custom'; };
* const obj1 = new CustomProto();
* obj1.a = 1;
* const obj2 = { b: 2 };
* const result = mergekit([obj1, obj2]);
* // result: { a: 1, b: 2, customMethod: [Function] }
*/
declare function mergekit(objects: object[] | object, options?: Partial<MergekitOptions>): any;
export { mergekit };