A JavaScript/TypeScript utility library for merging objects with optional deep cloning and recursive merging capabilities.
npx @tessl/cli install tessl/npm-merge@2.1.0Merge provides comprehensive object merging utilities for JavaScript and TypeScript applications. It enables developers to merge objects with optional deep cloning and recursive merging capabilities, featuring built-in prototype pollution protection and flexible API patterns.
npm install mergeimport merge from "merge";For CommonJS:
const merge = require("merge");Named imports (alternative approach):
import { merge, recursive, clone, isPlainObject } from "merge";import merge from "merge";
// Basic shallow merge using default export
const result1 = merge({ a: 1 }, { b: 2 });
// Result: { a: 1, b: 2 }
// Merge with cloning (first parameter true)
const original = { a: 1 };
const result2 = merge(true, original, { b: 2 });
// Result: { a: 1, b: 2 }, original unchanged
// Recursive merge using namespace property
const result3 = merge.recursive(
{ level: { value: 1 } },
{ level: { str: 'hello' } }
);
// Result: { level: { value: 1, str: 'hello' } }
// Deep clone using namespace property
const cloned = merge.clone({ a: { b: 1 } });
// Alternative: using named imports
import { merge as mergeFunc, recursive, clone } from "merge";
const result4 = mergeFunc({ a: 1 }, { b: 2 });
const result5 = recursive({ a: { b: 1 } }, { a: { c: 2 } });
// result4: { a: 1, b: 2 }
// result5: { a: { b: 1, c: 2 } }The primary merge function exported as default. Identical to the named merge function but also provides namespace access to other functions.
function main(clone: boolean, ...items: any[]): any;
function main(...items: any[]): any;When the first parameter is true, objects are cloned before merging to prevent mutation of input objects. Otherwise, the first object in the arguments is mutated and returned.
Parameters:
clone (boolean, optional): When true, clones objects before merging...items (any[]): Objects to merge togetherReturns: Merged object
Namespace Properties:
main.clone - Reference to clone functionmain.isPlainObject - Reference to isPlainObject functionmain.recursive - Reference to recursive functionPerforms shallow merging of objects with optional cloning.
function merge(clone: boolean, ...items: any[]): any;
function merge(...items: any[]): any;When the first parameter is true, objects are cloned before merging to prevent mutation of input objects. Otherwise, the first object in the arguments is mutated and returned.
Parameters:
clone (boolean, optional): When true, clones objects before merging...items (any[]): Objects to merge togetherReturns: Merged object
Performs deep/recursive merging of nested objects with optional cloning. Available as both a named export and as a namespace property on the main function.
function recursive(clone: boolean, ...items: any[]): any;
function recursive(...items: any[]): any;Recursively merges nested objects instead of replacing them entirely. Automatically filters dangerous prototype pollution keys (__proto__, constructor, prototype).
Parameters:
clone (boolean, optional): When true, clones objects before merging...items (any[]): Objects to recursively mergeReturns: Recursively merged object
Deep clones objects and arrays, preserving nested structures. Available as both a named export and as a namespace property on the main function.
function clone<T>(input: T): T;Creates a deep copy of the input, handling nested objects and arrays recursively. Primitive values are returned as-is.
Parameters:
input (any): Object, array, or primitive to cloneReturns: Deep cloned copy of the input
Checks if input is a plain object (not array, null, or primitive). Available as both a named export and as a namespace property on the main function.
function isPlainObject(input: any): input is Object;Type predicate function that determines if the input is a plain object suitable for merging operations.
Parameters:
input (any): Value to checkReturns: true if input is a plain object, false otherwise
For TypeScript users, the package includes complete type definitions:
// Main function with overloads
declare function main(clone: boolean, ...items: any[]): any;
declare function main(...items: any[]): any;
// Named export functions
declare function merge(clone: boolean, ...items: any[]): any;
declare function merge(...items: any[]): any;
declare function recursive(clone: boolean, ...items: any[]): any;
declare function recursive(...items: any[]): any;
declare function clone<T>(input: T): T;
declare function isPlainObject(input: any): input is Object;
// Main function namespace properties
declare namespace main {
var clone: typeof clone;
var isPlainObject: typeof isPlainObject;
var recursive: typeof recursive;
}The merge package includes built-in protection against prototype pollution attacks:
__proto__, constructor, prototypeThe package gracefully handles invalid inputs:
null, undefined, arrays, and primitives are ignored{}