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

collection-deep-merge

v1.3.0

Published

Deep merge array of objects by passed key. Objects with same key vals will merge together

Downloads

16

Readme

collection-deep-merge Build Status

Genereal purpose arrays of objects by key merger.

Ready for browsers (const, let compatible) and node.js v4+. IE11+, FF36+, Safari5.1+, Android

Works well as redux store helper.

Extensible via custom merger and comparator (Dependency Injection factory). Look more below.

It does not mutate arguments, it returns a new collection (Immutability).

usage

Open test.js with examples

const mergeById = mergeCollectionsBy('id')

//
// Deep merge object-items
const list1 = [ { a: 1, b: { c: 1 } }, { a: 2, b: { d: 2 } } ];
const list2 = [ { a: 1, b: { c: 3 } }, { a: 2, b: { c: 4 } } ];
const merged = [ { a: 1, b: { c: 3 } }, { a: 2, b: { c: 4, d: 2 } } ];

mergeById(list, [ item ]) ==== merged // deep equal

//
// Merge item with a collection
const list = [ { a: 1, b: 2 }, { a: 2, d: 4 } ];
const item = { a: 1, c: 3 };
const merged = [{ a: 1, b: 2, c: 3 }, { a: 2, d: 4 }]

mergeById(list, [ item ]) ==== merged // deep equal

tips & tricks

Custom item[key]s comparators and items mergers

const mergeCollectionsBy = require('collection-deep-merge');
const is = (a, b) => a !== undefined && a === b;

// You can merge in a shallow manner
// or implement very custom strategy
const merge = (a, b) => {/* custom merger */};

const mergeDeepById = mergeCollectionsBy('id', { is, merge });

Shallow merge

import mergeCollectionsBy, { mergeShallow } = 'collection-deep-merge';
const mergeShallowById = mergeCollectionsBy('id', { merge: mergeShallow });

Nested collections

You can implement custom merger based on const { mergeDeep } = require('collection-deep-merge') with an especial behaviour on nested collections.

You should support followed api:

  • merger is immutable
  • merger invokes with (Object a, Object b) args
  • merger returns Object c

Alternatives

array-join

collection-deep-merge like its fullJoin with deep item-objects merge

Map from ES6

New native Map has all you need:

  • preserve order
  • O(1) access
  • iterable

You can implement tiny union function (w/o deep items merging):

function union(map1, map2) {
  return new Map([...map1, ...map2]);
}

or you can use the collection-deep-merge if you want to merge items deeply O(n*m) + O(?) complexity (n*m not so good; ? stands for deep merge).

import mergeArrays from 'collection-deep-merge';
const mergeById = mergeArrays('id', { eq: (item1, item2) => {} });
const mergeMapsById = (map1, map2) => {
  mergeById([...map1], [...map2])
};

or ... // todo: create map-deep-union library special for Maps with O(n)+O(?) complexity.