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

@composi/merge-objects

v1.3.4

Published

Function to merge objects together. Can take one to many object. If only one object is used, it is cloned. merge does deep cloning of objects.

Downloads

9

Readme

@composi/merge-objects

This function takes objects and returns a new object with all of their properties. The last object's properties will replace those of the earlier when these have the same name. Since arrays are also objects, this will merge a series of arrays together.

Merge does a deep copy of objects, but ignores non-iterable properties.

Note:

Passing in non-object values, such as string or numbers, will result in unpredictable results. Merge must be used only for combining objects or arrays. Although this handles objects or arrays, you cannot merge objects and arrays together, this will create an unexecpte result.

Install

npm install --save-dev @composi/merge-objects

Using

Merge two objects:

import { mergeObjects } from '@composi/merge-objects'

const obj1 = {name: 'Mary'}
const obj2 = {job: 'project manager'}
const person = mergeObjects(obj1, obj2)
// returns {name: 'Mary', job: 'project manager'}

Clone an Object

You can clone an object with merge. Just pass in the object. The return object will be a clone:

import { mergeObjects } from '@composi/merge-objects'

const obj1 = {name: 'Joe', job: 'mechanic'}
const obj2 = mergeObjects(obj1)
obj1 === obj2 // returns false

Merge Arrays Together

You can use mergeObjects to merge any number of arrays together. This is a deep clone, which means you can use it safely with arrays of objects.


  const arr1 = [{name: 'Joe'}, {name: 'Jane'}]
  const arr2 = [{name: 'Mary'}, {name: 'Sam'}]
  const arr3 = mergeObjects(arr1, arr2)
  // arr3 equals [{name: 'Joe'}, {name: 'Jane'}, {name: 'Mary'}, {name: 'Sam'}])
  arr1[0].name = 'Joseph' // [{name: 'Joseph'}, {name: 'Jane'}]
  arr2[1].name = 'Samuel' // [{name: 'Mary'}, {name: 'Samuel'}]
  // The above changes do not affect arr3:
  // [{name: 'Joe'}, {name: 'Jane'}, {name: 'Mary'}, {name: 'Sam'}])

Clone an Array

If you want to clone an array, just pass it as the argument:

const arr1 = [{name: 'Joe'}, {name: 'Jane'}]
// Create clone of arr1:
const clonedArr1 = mergeObjects(arr1)
arr1[0].name = 'Joseph'
arr2[0].name // 'Joe'