What is mixme?
Mixme is a utility library for deep merging of objects. It allows you to combine multiple objects into one, handling nested properties and arrays gracefully.
What are mixme's main functionalities?
Deep Merge
This feature allows you to deeply merge two or more objects. Nested properties are merged recursively.
const mixme = require('mixme');
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { b: { d: 3 } };
const result = mixme.merge(obj1, obj2);
console.log(result); // { a: 1, b: { c: 2, d: 3 } }
Array Merge
This feature allows you to merge arrays within objects. The arrays are concatenated.
const mixme = require('mixme');
const obj1 = { a: [1, 2] };
const obj2 = { a: [3, 4] };
const result = mixme.merge(obj1, obj2);
console.log(result); // { a: [1, 2, 3, 4] }
Custom Merge Function
This feature allows you to provide a custom merge function to define how values should be combined.
const mixme = require('mixme');
const obj1 = { a: 1, b: 2 };
const obj2 = { a: 3, b: 4 };
const customMerge = (a, b) => a + b;
const result = mixme.merge(obj1, obj2, customMerge);
console.log(result); // { a: 4, b: 6 }
Other packages similar to mixme
lodash
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. It includes a `merge` function similar to mixme's deep merge, but also offers many other utilities.
deepmerge
Deepmerge is a library specifically designed for deep merging of JavaScript objects. It is similar to mixme in its core functionality but focuses solely on merging, without additional utilities.
merge
Merge is a simple utility for merging objects. It provides basic deep merge functionality but lacks some of the advanced features and customizability of mixme.
Node.js mixme
Merge multiple object recursively. The last object takes precedence over the
previous ones. Only objects are merged. Arrays are overwritten.
API
The API is minimalist, pass as many arguments as you wish, they will all be
merged.
The default function exported is immutable. Simply pass an empty object
and get the return result to achieve immutability.
mixme({a: '1'}, {b: '2'});
If you wish to enrich an object, pass it as first argument to the mutate
function:
obj = {a: '1'};
mixme.mutate(obj, {b: '2'});
Exemple
Merge an existing object with a second one:
obj1 = { a_key: 'a value', b_key: 'b value'};
obj2 = { b_key: 'new b value'};
result = misc.merge obj1, obj2
assert.eql result, obj1
assert.eql obj1.b_key, 'new b value'
Create a new object from two objects:
obj1 = { a_key: 'a value', b_key: 'b value'}
obj2 = { b_key: 'new b value'}
result = misc.merge {}, obj1, obj2
assert.eql result.b_key, 'new b value'
Testing
Clone the repo, install the development dependencies and run the tests:
git clone http://github.com/wdavidw/node-mixme.git .
npm install
make test