@sky0014/deep-merge
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

deep-merge

Merge second object to first object just like Object.assign, best to use it with immer.

Install

npm install @sky0014/deep-merge

Usage

import produce from "immer";
import { deepMerge } from "@sky0014/deep-merge";

const a = { a: 1, b: 2, c: { hello: "world" } };

const b = produce(a, (draft) => {
  Object.assign(draft, { c: { hello: "world" } });
});

const c = produce(a, (draft) => {
  deepMerge(draft, { c: { hello: "world" } }); // equal to draft.c.hello = "world"
});

const d = produce(a, (draft) => {
  deepMerge(draft, { c: {} }); // d = { a: 1, b: 2, c: { } }
});

console.log(a === b); // false 内容未变,引用改变
console.log(a === c); // true  内容未变,引用未变
console.log(a === d); // false 内容改变,引用改变

const e = produce(a, (draft) => {
  return deepMerge(draft, { a: 1 }, true); // replace(NOT merge) draft
}); // e = { a: 1 }

Publish

If your first time publish a package, login first:

npm login --registry=http://registry.npmjs.org

Then you can publish:

npm run pub

Readme

Keywords

none

Package Sidebar

Install

npm i @sky0014/deep-merge

Weekly Downloads

3

Version

1.0.0

License

none

Unpacked Size

6.32 kB

Total Files

8

Last publish

Collaborators

  • sky0014