or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-mixin-deep

Deeply mix object properties into the first object without cloning, with zero dependencies

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/mixin-deep@2.0.x

To install, run

npx @tessl/cli install tessl/npm-mixin-deep@2.0.0

index.mddocs/

Mixin Deep

Mixin 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.

Package Information

  • Package Name: mixin-deep
  • Package Type: npm
  • Language: JavaScript (ES6+)
  • Installation: npm install mixin-deep

Core Imports

const mixinDeep = require("mixin-deep");

For ES modules (requires transpiler/bundler):

import mixinDeep from "mixin-deep";

Basic Usage

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 }
// }

Capabilities

Deep Object Mixing

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:

  • In-place modification: The target object is modified directly, no cloning occurs
  • Deep merging: Nested objects are recursively merged
  • Multiple sources: Accepts any number of source objects as arguments
  • Property precedence: Later sources override earlier ones for primitive values
  • Nested preservation: Existing nested objects are preserved and extended
  • Security protection: Built-in prototype pollution protection

Behavior Details:

  • Object handling: Only plain objects and functions are deeply mixed
  • Array handling: Arrays are treated as primitive values and completely replaced
  • Special objects: RegExp, Date, and custom constructor objects are treated as primitives
  • Null/undefined sources: Ignored gracefully without errors
  • Security filtering: Dangerous keys (__proto__, constructor, prototype) are filtered out

Usage 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 }

Security Features

Mixin Deep includes built-in protection against prototype pollution attacks by filtering out dangerous property keys:

  • __proto__
  • constructor
  • prototype

This makes it safe to use with untrusted data sources while maintaining the convenience of deep object mixing.

Common Use Cases

  • Configuration merging: Combining default settings with user preferences
  • State management: Updating nested application state objects
  • API response processing: Merging data from multiple API endpoints
  • Template systems: Combining template data with runtime values
  • Plugin systems: Merging plugin configurations with base settings