Deeply mix object properties into the first object without cloning, with zero dependencies
npx @tessl/cli install tessl/npm-mixin-deep@2.0.0Mixin Deep provides a lightweight utility for deeply mixing object properties into the first object without cloning. Unlike merge-deep, it modifies the target object in place and has zero dependencies, making it ideal for configuration merging, state updates, and object composition scenarios.
npm install mixin-deepconst mixinDeep = require("mixin-deep");For ES modules (requires transpiler/bundler):
import mixinDeep from "mixin-deep";const mixinDeep = require("mixin-deep");
// Basic object mixing
const target = { a: { foo: true } };
const source1 = { a: { bar: true } };
const source2 = { a: { baz: true } };
const result = mixinDeep(target, source1, source2);
console.log(result);
// => { a: { foo: true, bar: true, baz: true } }
// Configuration merging example
const defaultConfig = {
server: { port: 3000, host: "localhost" },
features: { auth: false }
};
const userConfig = {
server: { port: 8080 },
features: { auth: true, logging: true }
};
const finalConfig = mixinDeep({}, defaultConfig, userConfig);
console.log(finalConfig);
// => {
// server: { port: 8080, host: "localhost" },
// features: { auth: true, logging: true }
// }Deeply mixes properties from multiple source objects into a target object, preserving nested structure and modifying the target in place.
/**
* Deeply mix the properties of objects into the first object
* @param {Object|Function} target - The target object to mix properties into (objects and functions only)
* @param {...(Object|Function|null|undefined)} sources - One or more source objects to mix from
* @returns {Object|Function} The modified target object
*/
function mixinDeep(target, ...sources);Key Features:
Behavior Details:
__proto__, constructor, prototype) are filtered outUsage Examples:
const mixinDeep = require("mixin-deep");
// Deep nested object mixing
const obj1 = {
config: {
database: { host: "localhost", port: 5432 },
cache: { enabled: false }
}
};
const obj2 = {
config: {
database: { port: 3306, ssl: true },
cache: { enabled: true, ttl: 300 }
}
};
const result = mixinDeep(obj1, obj2);
// obj1 is now:
// {
// config: {
// database: { host: "localhost", port: 3306, ssl: true },
// cache: { enabled: true, ttl: 300 }
// }
// }
// Using empty object to avoid modifying originals
const combined = mixinDeep({}, obj1, obj2);
// Handling arrays (replaced, not merged)
const withArrays = mixinDeep(
{ items: [1, 2, 3] },
{ items: [4, 5, 6] }
);
// Result: { items: [4, 5, 6] }
// Sparse object handling
const sparse = mixinDeep({}, undefined, { a: 1 }, null, { b: 2 });
// Result: { a: 1, b: 2 }Mixin Deep includes built-in protection against prototype pollution attacks by filtering out dangerous property keys:
__proto__constructorprototypeThis makes it safe to use with untrusted data sources while maintaining the convenience of deep object mixing.