Mixme Object Merger
Mixme is a JavaScript/TypeScript library (currently at v2.0.2) designed for recursively merging and manipulating JavaScript objects. It provides immutable merging via `merge` and mutable in-place modification via `mutate`. Key differentiators include its zero-dependency footprint, minimal size, and pure function approach for `merge`. It offers comprehensive TypeScript type definitions and supports both ES Modules (ESM) and CommonJS environments. While a specific release cadence isn't explicitly published, the project appears actively maintained with a streamlined release process via GitHub Actions. A notable behavior is that arrays are always overwritten during merges, not recursively combined, which is a common point of confusion for users expecting deep array merging.
Common errors
-
Cannot use import statement outside a module
cause Attempting to use `import` syntax in a CommonJS file or a Node.js project not configured for ES Modules.fixEnsure your `package.json` contains `"type": "module"` for ESM, or rename `.js` files using `import` to `.mjs`. Alternatively, if staying with CommonJS, use `const { merge } = require('mixme');`. -
TypeError: mixme.merge is not a function
cause This typically happens when trying to `require('mixme')` and then calling `mixme.merge()` without destructuring or accessing the named export properly in a CommonJS environment.fixIn CommonJS, you must destructure the named export: `const { merge } = require('mixme');` or access it as a property: `const mixme = require('mixme'); const result = mixme.merge(...);`. -
ReferenceError: require is not defined
cause This occurs when trying to use `require()` in an ES Module context (e.g., a file with `"type": "module"` or `.mjs` extension).fixSwitch to `import` syntax: `import { merge } from 'mixme';` instead of `const { merge } = require('mixme');`. If legacy CommonJS modules are needed in an ESM project, consider dynamic `import()` or specific tools like `createRequire`.
Warnings
- gotcha When merging objects with `merge` or `mutate`, arrays are always overwritten by the subsequent object's array value, not recursively merged. This differs from some other deep merge utilities.
- gotcha The `mutate` function directly modifies its first argument. If you need to preserve the original object, always pass a `clone()` of the original object as the first argument to `mutate`.
- breaking While `mixme` v2 supports both ESM and CommonJS, migrating older CommonJS-only projects to `import` statements may require configuring 'type': 'module' in `package.json` or changing file extensions to `.mjs` for Node.js environments.
Install
-
npm install mixme -
yarn add mixme -
pnpm add mixme
Imports
- merge
const merge = require('mixme').merge;import { merge } from 'mixme'; - mutate
import mutate from 'mixme';
import { mutate } from 'mixme'; - clone
const { clone } = require('mixme');import { clone } from 'mixme';
Quickstart
import { merge, mutate, clone, snake_case } from 'mixme';
interface Config {
appName: string;
version: string;
settings: {
theme: string;
notifications: boolean;
};
features: string[];
}
const defaultAppConfig: Config = {
appName: 'My App',
version: '1.0.0',
settings: {
theme: 'dark',
notifications: true,
},
features: ['dashboard', 'reports'],
};
const userConfig: Partial<Config> = {
appName: 'My Custom App',
settings: {
theme: 'light',
},
features: ['dashboard', 'analytics'],
};
// Immutable merge: creates a new object
const finalConfig = merge(defaultAppConfig, userConfig);
console.log('Final Config (merged):', finalConfig);
// Expected: { appName: 'My Custom App', version: '1.0.0', settings: { theme: 'light', notifications: true }, features: ['dashboard', 'analytics'] }
// Mutable merge: modifies the first object
const baseConfigCopy = clone(defaultAppConfig); // Clone to avoid modifying original defaultAppConfig
mutate(baseConfigCopy, userConfig);
console.log('Mutated Config:', baseConfigCopy);
// Expected: { appName: 'My Custom App', version: '1.0.0', settings: { theme: 'light', notifications: true }, features: ['dashboard', 'analytics'] }
// Example of snake_case utility
const camelCaseObject = { userFirstName: 'John', userLastName: 'Doe' };
const snakeCaseObject = snake_case(camelCaseObject);
console.log('Snake Case Object:', snakeCaseObject);
// Expected: { user_first_name: 'John', user_last_name: 'Doe' }