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

2.0.0 • Public • Published

@jalik/deep-extend

GitHub package.json version Build Status GitHub last commit GitHub issues GitHub npm

Deep merge any objects.

Sandbox

Play with the lib here: https://codesandbox.io/s/jalik-deep-extend-demo-wyeo99?file=/src/index.js

Installing

npm i -P @jalik/deep-extend
yarn add @jalik/deep-extend

Merging deep objects

The following code shows how to merge objects without losing values that are not defined in objects to merge.

import deepExtend from "@jalik/deep-extend";

const defaultColors = {
  cold: {
    blue: '#0000FF',
    green: '#00FF00',
  },
  hot: {
    red: '#FF0000',
    yellow: '#FFFF00',
  },
};

const customColors = {
  cold: {
    blue: '#48C2ED',
  },
  hot: {
    yellow: '#E6CB5F',
  },
};

// Merge all colors into a new object.
// The final colors will have custom blue and yellow colors,
// but the other colors will be the default ones.
const result = deepExtend({}, defaultColors, customColors);

The result:

{
  "cold": {
    "blue": "#48C2ED",
    "green": "#00FF00"
  },
  "hot": {
    "red": "#FF0000",
    "yellow": "#E6CB5F"
  }
}

Merging arrays

See below how it is easy to merge arrays recursively.

import deepExtend from "@jalik/deep-extend";

const a = [1, [2, [3]]];
const b = [undefined, [4, [undefined, 5], 6], 7];

const result = deepExtend([], a, b);

The result:

[
  1,
  [
    4,
    [
      3,
      5
    ],
    6
  ],
  7
]

Changelog

History of releases is in the changelog.

License

The code is released under the MIT License.

Readme

Keywords

Package Sidebar

Install

npm i @jalik/deep-extend

Weekly Downloads

519

Version

2.0.0

License

MIT

Unpacked Size

15.6 kB

Total Files

11

Last publish

Collaborators

  • jalik