npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@corex/deepmerge

v4.0.43

Published

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

Downloads

1,442,738

Readme


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

| property | default | description | | -------------- | --------- | ---------------------------------------------------------------------------------------------------- | | arrayMergeType | combine | Merge 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,
//   },
// }