@corex/deepmerge

中文文档
4.0.43发布于 2023/04/12MITTypeScriptESM + CJS零依赖近 30 天下载 291.4万

A zero dependency object merger with typescript support and built in common merge utilities.

原始使用文档
文档目录10 节
  1. title: '@corex/deepmerge' custom_edit_url: 'https://github.…
  2. Installation
  3. Usage
  4. Merge Options
  5. Simple Object Merge
  6. Array Merge
  7. Complex Merge
  8. Array merge types
  9. Overwrite merge
  10. Custom merge function

title: '@corex/deepmerge' custom_edit_url: 'https://github.com/iamvishnusankar/corex/edit/master/packages/deepmerge/README.md'

A zero dependency object merger with typescript support and built in common merge utilities. Support merging of n number of objects

Installation

yarn add @corex/deepmerge

Usage

import { merge } from '@corex/deepmerge'

merge([obj1, obj2, obj3], { options })

Merge Options

propertydefaultdescription
arrayMergeTypecombineMerge two array by concatenation and remove duplicates. Available options are combine, overwrite
arrayMerge-Custom merge function to handle array merging.

Simple Object Merge

const obj1 = {
  a: 1,
  b: {
    c: 2,
  },
}

const obj2 = {
  a: 2,
  b: {
    c: 4,
    d: 5,
  },
}

const result = merge([obj1, obj2])
console.log(result)

// {
//   a: 2,
//   b: {
//     c: 4,
//     d: 5,
//   },
// }

Array Merge

const arr1 = {
  a: [1, 2, 3],
}
const arr2 = {
  a: [3, 4, 5],
}

const result = merge([arr1, arr2])
console.log(result)

// result = {
//   a: [1, 2, 3, 4, 5],
// }

Complex Merge

const obj1 = {
  a: 2,
  b: (x: number, y: number) => x + y,
  c: [1, 2, 4],
  d: {
    e: [4, 5, 7],
  },
}

console.log(obj1.b(7, 5)) // ==> 12

const obj2 = {
  a: 10,
  b: (x: number, y: number) => x / y,
  c: [20, 30],
  d: {
    e: [4, 7, 5],
    f: {
      g: 'another',
    },
  },
}

const obj3 = {
  b: (x: number, y: number) => x * y,
  c: [1, 2, 4],
  d: {
    e: [4, 7, 5],
  },
}

const result = merge([obj1, obj2, obj3])

console.log(result.b(7, 5)) // ==> 35

Array merge types

By default @corex/deepmerge uses combine merge. However user can implement their own methods to perform merge or use the built in overwrite.

Overwrite merge

const obj1 = {
  a: [1, 2, 3],
  b: ['c', 'd'],
  c: {
    d: [1, 2],
  },
}

const obj2 = {
  a: [1, 7, 6],
  b: ['e', 'f'],
  c: {
    d: [9, 10],
  },
}

const obj3 = {
  a: [20, 40],
}

const result = merge([obj1, obj2, obj3], {
  arrayMergeType: 'overwrite',
})
console.log(result)

// result = {
//   a: [20, 40],
//   b: ['e', 'f'],
//   c: {
//     d: [9, 10],
//   },
// }

Custom merge function

const obj1 = {
  a: ['1', '2'],
  c: {
    d: [1000, 500],
  },
}

const obj2 = {
  a: ['4', '5'],
  c: {
    d: [60, 80],
  },
}

const customMergeFn = (_: any[], __: any[]) => 42

const result = merge([obj1, obj2], {
  arrayMerge: customMergeFn,
})
console.log(result)

// {
//   a: 42,
//   c: {
//     d: 42,
//   },
// }

版本与发布记录

@corex/deepmerge 共发布 110 个版本,首次发布于 2020/08/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.43latest2023/04/123 年前
4.0.372023/01/103 年前
4.0.292022/06/304 年前
4.0.262022/06/204 年前
4.0.242022/06/194 年前
4.0.222022/06/134 年前

关于 @corex/deepmerge 的常见问题

@corex/deepmerge 是什么?

A zero dependency object merger with typescript support and built in common merge utilities.

如何安装 @corex/deepmerge?

在项目目录运行 npm install @corex/deepmerge,或使用 pnpm add / yarn add / bun add。

@corex/deepmerge 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@corex/deepmerge 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@corex/deepmerge 可以商用吗?

@corex/deepmerge 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@corex/deepmerge 的下载量有多少?

根据 npm 官方下载统计,@corex/deepmerge 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,914,095 次,日均约 97,137 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@corex/deepmerge 还在维护吗?

最新版本 4.0.43 发布于 2023/04/12(3 年前);至今共发布 110 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/08/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。