What is merge?
The merge npm package is a utility that allows you to merge multiple objects into one. It is useful when you want to combine configurations, settings, or other data structures in JavaScript. It performs a deep merge by default, meaning that it recursively merges properties of objects, but it can also be configured to perform a shallow merge.
What are merge's main functionalities?
Deep Merge
This feature allows for the deep merging of objects, meaning that nested objects will also be merged together. The code sample demonstrates merging two objects with nested properties.
{"const merge = require('merge');
const object1 = { a: 1, b: { c: 2 } };
const object2 = { b: { d: 3 }, e: 4 };
const mergedObject = merge(object1, object2);
console.log(mergedObject); // Output: { a: 1, b: { c: 2, d: 3 }, e: 4 }"}
Shallow Merge
This feature allows for the shallow merging of objects, which means that it will not recursively merge nested objects. The code sample demonstrates merging two objects without combining the nested properties.
{"const merge = require('merge');
const object1 = { a: 1, b: { c: 2 } };
const object2 = { b: { d: 3 }, e: 4 };
const mergedObject = merge.recursive(false, object1, object2);
console.log(mergedObject); // Output: { a: 1, b: { d: 3 }, e: 4 }"}
Other packages similar to merge
lodash.merge
Lodash's merge function is similar to merge, offering deep merge capabilities. It is part of the larger Lodash library, which provides a wide range of utility functions for working with arrays, numbers, objects, strings, etc. Lodash is well-known for its performance and reliability.
deepmerge
Deepmerge is another npm package that provides deep merge functionality. It is designed to be more flexible than merge, allowing for custom merge strategies for specific fields and handling of arrays and other types that merge might not support out of the box.
extend
Extend is a jQuery-inspired package that can perform both deep and shallow merges. It is lightweight and straightforward, but it does not offer as much control over the merge process as merge or deepmerge.
object-assign
Object-assign is a polyfill for the Object.assign method, which performs a shallow merge of objects. It is built to match the ES6 specification for Object.assign and is useful for simple merging tasks where deep merge is not required.
Merge
Merge multiple objects into one, optionally creating a new cloned object.
Similar to the jQuery.extend but more flexible. Works in Node.js and the
browser.
Node.js Usage
npm install merge --save
var merge = require('merge'), original, cloned;
console.log(merge({one:'hello'}, {two: 'world'}));
original = { x: { y: 1 } };
cloned = merge(true, original);
cloned.x.y++;
console.log(original.x.y, cloned.x.y);
console.log(merge.recursive(true, original, { x: { z: 2 } }));
Browser Usage
<script src="https://cdn.jsdelivr.net/gh/yeikos/js.merge/merge.js"></script>
<script>
var original, cloned;
console.log(merge({one:'hello'}, {two: 'world'}));
original = { x: { y: 1 } };
cloned = merge(true, original);
cloned.x.y++;
console.log(original.x.y, cloned.x.y);
console.log(merge.recursive(true, original, { x: { z: 2 } }));
</script>
Tests
npm test