npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@hyperse/deep-merge

v1.0.1

Published

A TypeScript library for performing deep merges of objects with advanced configuration options.

Readme

@hyperse/deep-merge

A TypeScript library for performing deep merges of objects with advanced configuration options. Unlike Object.assign(), this library creates new objects without mutating the original target, and provides fine-grained control over how different types of values are merged.

Features

  • Immutable Merging: Target objects are never mutated; new objects are always returned
  • Deep Recursive Merging: Nested objects are merged recursively
  • Type Safety: Full TypeScript support with generic types
  • Class Instance Handling: Special handling for class instances vs plain objects
  • Array Replacement: Arrays are treated as single values and replaced (not merged)
  • Undefined Value Control: Optional merging of undefined values
  • Fast Deep Cloning: Efficient deep cloning utility for simple objects

Installation

npm install @hyperse/deep-merge
# or
yarn add @hyperse/deep-merge
# or
pnpm add @hyperse/deep-merge

API Reference

mergeOptions<T>(target: T, source: DeepPartial<T>, mergeUndefined?: boolean, depth?: number): T

Performs a deep merge of two objects, where the source object's properties are merged into the target object.

Parameters

  • target: T - The target object to merge into
  • source: DeepPartial<T> - The source object to merge from
  • mergeUndefined?: boolean - Whether to merge undefined values (default: false)
  • depth?: number - The current depth of the merge (used internally, default: 0)

Returns

  • T - A new object containing the merged properties

Behavior

  • Objects: Recursively merged if they are plain objects (not class instances)
  • Arrays: Replaced entirely (not merged)
  • Class Instances: Replaced entirely (not merged)
  • Primitives: Replaced by source values
  • Undefined Values: Only merged if mergeUndefined is true

simpleDeepClone<T>(input: T): T

An extremely fast function for deep-cloning objects that contain only simple values (primitives, arrays, and nested simple objects).

Parameters

  • input: T - The input to clone

Returns

  • T - The cloned input

Behavior

  • Primitives: Returned as-is
  • Arrays: Deep cloned recursively
  • Plain Objects: Deep cloned recursively
  • Class Instances: Returned as-is (not cloned)
  • Null/Undefined: Returned as-is

Utility Functions

isObject(item: any): item is object

Checks if an item is a plain object (not an array or null).

isClassInstance(item: any): boolean

Checks if an item is a class instance (has a constructor that's not Object).

Type Definitions

DeepPartial<T>

A utility type that makes all properties of T optional recursively.

Usage Examples

Basic Object Merging

import { mergeOptions } from '@hyperse/deep-merge';

const target = {
  name: 'John',
  age: 30,
  address: {
    city: 'New York',
    country: 'USA',
  },
  hobbies: ['reading', 'swimming'],
};

const source = {
  age: 31,
  address: {
    city: 'Los Angeles',
  },
  hobbies: ['coding'],
};

const result = mergeOptions(target, source);
// Result:
// {
//   name: 'John',
//   age: 31,
//   address: {
//     city: 'Los Angeles',
//     country: 'USA'
//   },
//   hobbies: ['coding']
// }

Plugin Configuration Merging

import { mergeOptions } from '@hyperse/deep-merge';

const defaultConfig = {
  dts: true,
  input: { index: 'src/index.ts' },
  logSilent: false,
  modularImports: [],
  plugin: {
    extraPlugins: [],
    pluginConfigs: {
      multiInputOptions: { fastGlobOptions: { ignore: [] } },
      replaceOptions: { 'process.env.NODE_ENV': '"production"' },
      aliasOptions: { entries: [] },
      nodeResolveOptions: {
        extensions: ['.js', '.ts', '.tsx', '.json', '.vue'],
      },
      jsonOptions: {},
      commonjsOptions: {},
      babelOptions: { usePreset: 'node' },
      terserOptions: true,
    },
  },
  funcs: {
    fn: () => {},
  },
};

const userConfig = {
  dts: {
    entryPointOptions: {
      libraries: { importedLibraries: ['@dimjs/utils'] },
    },
  },
  input: 'src/index.ts',
  plugin: {
    extraPlugins: [],
    pluginConfigs: {
      terserOptions: {
        came: 1,
      },
    },
  },
  output: { format: 'esm' },
};

const result = mergeOptions(defaultConfig, userConfig);

Handling Undefined Values

import { mergeOptions } from '@hyperse/deep-merge';

const target = {
  a: 1,
  b: { c: 2, d: 3 },
  e: 'hello',
};

const source = {
  a: undefined,
  b: { c: undefined, f: 4 },
  e: undefined,
};

// Without merging undefined values (default)
const result1 = mergeOptions(target, source);
// Result: { a: 1, b: { c: 2, d: 3, f: 4 }, e: 'hello' }

// With merging undefined values
const result2 = mergeOptions(target, source, true);
// Result: { a: undefined, b: { c: undefined, d: 3, f: 4 }, e: undefined }

Deep Cloning

import { simpleDeepClone } from '@hyperse/deep-merge';

const original = {
  user: { name: 'John', preferences: { theme: 'dark' } },
  settings: [1, 2, { nested: true }],
};

const clone = simpleDeepClone(original);

// Modifying the original doesn't affect the clone
original.user.name = 'Jane';
original.settings[0] = 999;

console.log(clone.user.name); // 'John'
console.log(clone.settings[0]); // 1

Class Instance Handling

import { mergeOptions } from '@hyperse/deep-merge';

class User {
  constructor(public name: string) {}
}

class Config {
  constructor(public value: number) {}
}

const target = {
  user: new User('John'),
  config: new Config(100),
  data: { x: 1, y: 2 },
};

const source = {
  user: new User('Jane'),
  data: { y: 3, z: 4 },
};

const result = mergeOptions(target, source);
// Class instances are replaced entirely
// result.user is the new User('Jane') instance
// result.data is { x: 1, y: 3, z: 4 }

Important Notes

Array Behavior

Arrays are treated as single values and are completely replaced during merging:

const target = { items: [1, 2, 3] };
const source = { items: [4, 5] };

const result = mergeOptions(target, source);
// result.items is [4, 5], not [1, 2, 3, 4, 5]

Class Instance Behavior

Class instances are not recursively merged - they are replaced entirely:

class MyClass {
  constructor(public value: number) {}
}

const target = { obj: new MyClass(1) };
const source = { obj: new MyClass(2) };

const result = mergeOptions(target, source);
// result.obj is the new MyClass(2) instance

Immutability

The target object is never mutated. A new object is always returned:

const target = { a: 1, b: { c: 2 } };
const source = { b: { c: 3 } };

const result = mergeOptions(target, source);
console.log(target === result); // false
console.log(target.b === result.b); // false

TypeScript Support

The library provides full TypeScript support with generic types:

interface Config {
  name: string;
  settings: {
    theme: string;
    debug: boolean;
  };
  plugins: string[];
}

const defaultConfig: Config = {
  name: 'default',
  settings: { theme: 'light', debug: false },
  plugins: [],
};

const userConfig: DeepPartial<Config> = {
  settings: { theme: 'dark' },
};

const result = mergeOptions(defaultConfig, userConfig);
// result is typed as Config

Performance Considerations

  • mergeOptions creates deep clones of the target object, so it's best suited for configuration merging rather than high-frequency operations
  • simpleDeepClone is optimized for simple objects and is much faster than JSON.parse/stringify methods
  • Class instances are not cloned to preserve their behavior and methods

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.