Skip to content

Latest commit

 

History

History
151 lines (112 loc) · 2.98 KB

README.md

File metadata and controls

151 lines (112 loc) · 2.98 KB

Build Status codecov Snyk Vulnerabilities for GitHub Repo npm bundle size NPM npm

简介(Summary)

Object Merge是一个用于JS对象合并的工具库,包含数组和对象的合并,实现对象间的深拷贝。

尤其是用于处理标准配置与本地配置的合并。

支持Node版本:>= 12.X

特性(Feature)

  • 轻量级
  • 无依赖
  • 纯JS实现

安装(Install)

  • npm安装
npm i fd-object-merge --save
  • cnpm安装
cnpm i fd-object-merge --save

API

objectMerge

类型

Function

参数

参数名 类型 必须 说明
targetObj any 目标对象
sourceObj any 源对象
options any 配置项
options.array.isConcat Boolean 若是数组(或对象属性为数组)时,数组是否进行合并。true:合并;false:不合并
options.mixAttr Boolean targetObjsourceObj属性存在不一致的情况时,是否进行属性合并。true:合并;false:不合并

使用(Usage)

默认配置示例

const objectMerge = require('fd-object-merge');

const targetObj = {
  name: 'zhangsan',
  age: 18
};

const sourceObj = {
  name: 'lisi',
  weight: 140,
};

const result = objectMerge(targetObj, sourceObj);

console.log(result);

/**
 * result = {
 *   name: 'zhangsan',
 *   age: 18,
 *   weight: 140
 * }
 */

数组不合并示例

const objectMerge = require('fd-object-merge');

const targetObj = {
  name: 'zhangsan',
  age: 18,
  class: ['Math', 'Chinese'],
};

const sourceObj = {
  name: 'lisi',
  weight: 140,
  class: ['English']
};

const options = {
  array: {
    isConcat: false, // 数组不合并
  }
};

const result = objectMerge(targetObj, sourceObj, options);

console.log(result);

/**
 * result = {
 *   name: 'zhangsan',
 *   age: 18,
 *   weight: 140,
 *   class: ['Math', 'Chinese']
 * }
 */

属性不合并示例

const objectMerge = require('fd-object-merge');

const targetObj = {
  name: 'zhangsan',
  age: 18,
  class: ['Math', 'Chinese'],
};

const sourceObj = {
  name: 'lisi',
  weight: 140,
  class: ['English']
};

const options = {
  mixAttr: false, // 属性不合并
};

const result = objectMerge(targetObj, sourceObj, options);

console.log(result);

/**
 * result = {
 *   name: 'zhangsan',
 *   age: 18,
 *   class: ['Math', 'Chinese', 'English']
 * }
 */