utils-merge
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
-
TypeError: merge is not a function
cause Incorrect import statement; `utils-merge` exports its function as a default export, not a named export.fixFor CommonJS: `const merge = require('utils-merge');`. For ESM: `import merge from 'utils-merge';`. -
Uncaught TypeError: Cannot set properties of undefined (reading 'foo')
cause Attempting to merge into a non-object destination (e.g., `merge(null, source)` or `merge(undefined, source)`), which is not supported.fixEnsure the first argument passed to `merge` is always a valid object (or an empty object literal if you intend to create a new one, e.g., `merge({}, source)`). -
My nested object properties are gone after merging!
cause The `utils-merge` function performs a shallow merge, replacing entire nested objects rather than merging their individual properties.fixThis is expected behavior for `utils-merge`. If deep merging is required, use a dedicated deep merge library or manually implement a recursive merge.
Warnings
- gotcha The `merge` function performs a shallow merge. This means that if both source and destination objects contain nested objects with the same key, the nested object from the source will completely overwrite the one in the destination, rather than recursively merging their properties.
- gotcha The `merge` function mutates the destination object directly. It does not return a new object with the merged properties. If you need to preserve the original destination object, you must explicitly clone it before calling `merge`.
- gotcha The package is minimally maintained since around 2017 (as indicated by copyright dates and last known updates). While stable for its intended simple use case, it may not receive updates for new JavaScript features, bug fixes, or security patches relevant to complex object manipulation.
- gotcha The package is primarily designed for CommonJS (Node.js) environments, as shown in its usage examples. While modern bundlers can handle it in ESM projects, direct `import` syntax might require specific configuration or careful usage if not properly transpiled, especially in older environments.
Install
-
npm install utils-merge -
yarn add utils-merge -
pnpm add utils-merge
Imports
- merge
const { merge } = require('utils-merge');const merge = require('utils-merge'); - merge
import { merge } from 'utils-merge';import merge from 'utils-merge';
- merge (TypeScript)
import * as merge from 'utils-merge';
import merge from 'utils-merge';
Quickstart
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));