@75lb/deep-merge 中文文档教程

发布于 3年前 浏览 23 项目主页 更新于 3年前

在 npm 上查看npm 模块下载Gihub repo dependentsGihub package dependentsNode.js CI覆盖状态js- standard-style

@75lb/deep-merge

将一个对象结构的值深度合并到另一个对象结构中。 类似于 Object.assign() 除了它处理对象结构的完整深度,而不仅仅是顶层。 对于合并配置很有用。

Synopsis

import deepMerge from '@75lb/deep-merge'

Simple

合并四个对象的典型示例。 输入:

deepMerge(
  { port: 8000, data: { animal: 'cow' } },
  { stack: ['one'] },
  { stack: ['two'], help: true },
  { data: { animal: 'bat', metal: 'iron' } }
)

结果

{
  port: 8000,
  stack: ['two'],
  help: true,
  data: { animal: 'bat', metal: 'iron' }
}

Arrays

空数组将被忽略且不会合并。输入:

deepMerge(
  { stack: ['one'] },
  { stack: [] }
)

结果:

{ stack: ['one'] }

但是,如果后面的数组包含一个或多个值,后面的数组将替换原始数组:

deepMerge(
  { stack: ['one'] },
  { stack: ['two'] }
)

结果:

{ stack: ['two'] }

Load anywhere

该库与 Node 兼容.js、Web 和任何风格的模块加载器。 它可以在任何地方本地加载,无需转译。

在 Node.js ECMAScript 模块中:

import deepMerge from '@75lb/deep-merge'

在现代浏览器 ECMAScript 模块中:

import deepMerge from './node_modules/@75lb/deep-merge/dist/index.mjs'

© 2021 Lloyd Brookes \<75pound@gmail.com>。

test-runner 测试。 由 jsdoc-to-markdown 记录。

view on npmnpm module downloadsGihub repo dependentsGihub package dependentsNode.js CICoverage Statusjs-standard-style

@75lb/deep-merge

Deep-merge the values of one object structure into another. Similar to Object.assign() except it processes the full depth of the object structure, not only the top level. Useful for merging config.

Synopsis

import deepMerge from '@75lb/deep-merge'

Simple

Typical example merging four objects. Input:

deepMerge(
  { port: 8000, data: { animal: 'cow' } },
  { stack: ['one'] },
  { stack: ['two'], help: true },
  { data: { animal: 'bat', metal: 'iron' } }
)

Result

{
  port: 8000,
  stack: ['two'],
  help: true,
  data: { animal: 'bat', metal: 'iron' }
}

Arrays

Empty arrays are ignored and not merged in. Input:

deepMerge(
  { stack: ['one'] },
  { stack: [] }
)

Result:

{ stack: ['one'] }

However, if the later array contains one or more values the later array will replace the original:

deepMerge(
  { stack: ['one'] },
  { stack: ['two'] }
)

Result:

{ stack: ['two'] }

Load anywhere

This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.

Within a Node.js ECMAScript Module:

import deepMerge from '@75lb/deep-merge'

Within an modern browser ECMAScript Module:

import deepMerge from './node_modules/@75lb/deep-merge/dist/index.mjs'

© 2021 Lloyd Brookes \<75pound@gmail.com>.

Tested by test-runner. Documented by jsdoc-to-markdown.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文