utils-merge

1.0.1 · maintenance · verified Sun Apr 19

The `utils-merge` package offers a singular utility function, `merge(destination, source)`, which facilitates the shallow combination of properties from a source object into a destination object. It operates by copying enumerable own properties from the source directly onto the destination, overwriting any existing properties with matching keys. The package is currently at version 1.0.1, reflecting a stable and mature, though minimally maintained, codebase since its last updates around 2017. Its core differentiator is its simplicity and direct mutable merging, suitable for scenarios where a lightweight, shallow merge is explicitly desired and object mutation is an acceptable side effect, rather than requiring deep cloning or immutability features found in more complex merging libraries.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the shallow merging behavior and direct object mutation of `utils-merge`, showing how it combines properties and handles nested objects by replacement.

const merge = require('utils-merge'); // CommonJS import for Node.js environments

// Define initial objects
let userProfile = {
  id: 'user123',
  name: 'Jane Doe',
  settings: {
    theme: 'dark',
    notifications: { email: true, sms: false }
  }
};

let defaultProfileSettings = {
  language: 'en-US',
  settings: {
    notifications: { email: false, push: true }, // This will completely replace userProfile.settings.notifications
    privacy: { dataSharing: false }
  },
  status: 'active'
};

console.log('Initial User Profile:', JSON.stringify(userProfile, null, 2));
console.log('Default Settings to Merge:', JSON.stringify(defaultProfileSettings, null, 2));

// Perform the merge operation
// WARNING: utils-merge performs a shallow merge and mutates the destination object.
// Nested objects from the source will replace the corresponding nested objects in the destination,
// rather than merging their properties recursively.
merge(userProfile, defaultProfileSettings);

console.log('\nUser Profile After Shallow Merge (destination mutated):', JSON.stringify(userProfile, null, 2));

// Example of shallow merge for nested objects:
let objA = { config: { timeout: 1000, retries: 3 } };
let objB = { config: { timeout: 5000, maxAttempts: 5 } };

console.log('\nBefore nested merge: objA =', JSON.stringify(objA));
merge(objA, objB);
console.log('After nested merge (config from objB replaced objA.config): objA =', JSON.stringify(objA));

// To create a new object without mutating the original 'userProfile' while using utils-merge:
// First, create a shallow copy of 'userProfile' (or an empty object),
// then merge the defaults into that copy.
let newMergedProfile = merge({}, userProfile);
merge(newMergedProfile, defaultProfileSettings); // Note: This example is illustrative. For true deep merge, other libs are needed.

console.log('\nNew merged profile (via cloning then merging):', JSON.stringify(newMergedProfile, null, 2));

view raw JSON →