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

@oleksii-pavlov/deep-merge

v1.2.3

Published

Utility that merges objects

Downloads

108

Readme

@oleksii-pavlov/deep-merge

This is a library for working with objects. It provides utilities to merge, update, clone and compare objects considering their nesting! This library is written in Typescript and its utilities save the typing of the objects. Functions which names are ended up with AndAssign mutate first argument!

Documentation

Utilities:

  • deepMerge
  • deepUpdate
  • deepMergeAndAssign
  • deepUpdateAndAssign
  • deepClone
  • deepCompare

Usage

deepMerge - for merging with type changing without mutating

Example:

import { deepMerge } from '@oleksii/deep-merge'

interface UserWithFirstName {
  firstName: string
}
interface UserWithLastName {
  lastName: string
}

// intersection type
type User = UserWithFirstName & UserWithLastName

const userWithFirstName: UserWithFirstName = {
  firstName: 'Oleksii'
}
const userWithLastName: UserWithLastName = {
  lastName: 'Pavlov'
}

// merge two objects
const user = deepMerge<User>(userWithFirstName, userWithLastName)

console.log(user) // { firstName: 'Oleksii', lastName: 'Pavlov' }

deepUpdate - for updating values without type changing without mutating

Example:

import { deepUpdate } from '@oleksii/deep-merge'

interface User {
  firstName: string
  lastName: string
  age: number
}

const user: User = {
  firstName: 'Oleksii'
  lastName: 'Pavlov'
  age: 30
}
const updatedUserData: Partial<User> = {
  age: 31
}

const updatedUser = deepUpdate(user, updatedUserData)

// updatedUser: User - type has not been changed
console.log(user) // not mutated: { firstName: 'Oleksii', lastName: 'Pavlov', age: 30 }
console.log(updatedUser) // updated: { firstName: 'Oleksii', lastName: 'Pavlov', age: 31 }

deepMergeAndAssign - for merging with type changing with mutating

Example:

import { deepMergeAndAssign } from '@oleksii/deep-merge'

interface UserWithFirstName {
  firstName: string
}
interface UserWithLastName {
  lastName: string
}

// intersection type
type User = UserWithFirstName & UserWithLastName

const userWithFirstName: UserWithFirstName = {
  firstName: 'Oleksii'
}
const userWithLastName: UserWithLastName = {
  lastName: 'Pavlov'
}

deepMergeAndAssign<User>(userWithFirstName, userWithLastName)

console.log(userWithFirstName) // userWithFirstName is mutated: { firstName: 'Oleksii', lastName: 'Pavlov' }

deepUpdateAndAssign - for updating values without type changing with mutating

Example:

import { deepUpdateAndAssign } from '@oleksii/deep-merge'

interface User {
  firstName: string
  lastName: string
  age: number
}

const user: User = {
  firstName: 'Oleksii'
  lastName: 'Pavlov'
  age: 30
}
const updatedUserData: Partial<User> = {
  age: 31
}

deepUpdateAndAssign(user, updatedUserData)

// user: User - type has not been changed
console.log(user) // user is mutated: { firstName: 'Oleksii', lastName: 'Pavlov', age: 31 }

deepClone - clones an object recursively

Example:

import { deepClone, deepCompare } from '@oleksii-pavlov/deep-merge'

const user = {
  name: 'Oleksii',
  surname: 'Pavlov',
  family: {
    numberOfSiblings: 2
  }
}

const clonedUser = deepClone(user)

console.log(user !== clonedUser) // true, different object
console.log(deepCompare(user, clonedUser)) // true, cloned object has the same structure
console.log(user.family !== clonedUser.family) // true, nested objects are recursively cloned as well

deepCompare compares objects (number of arguments in unlimited)

Example:

import { deepCompare } from '@oleksii-pavlov/deep-merge'

const user = {
  name: "Tom",
  address: {
    street: "Main street",
    apartments: 18
  }
}

const copiedUser = {
  name: "Tom",
  address: {
    street: "Main street",
    apartments: 18
  }
}

const userWithDifferentAddress = {
  name: "Tom",
  address: {
    street: "Main street",
    apartments: 21
  }
}

console.log(deepCompare(user, clonedUser)) // true as they are the same structured objects
console.log(deepCompare(user, userWithDifferentAddress)) // false as the addresses are different