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

group-by-with-sum

v2.0.0

Published

Collapses an array of objects at the specified object properties

Downloads

1,720

Readme

group-by-with-sum

Collapses an array of objects at the specified object properties

npm install group-by-with-sum
const groupBy = require('group-by-with-sum');

const arr = [
  { name: 'Vasya', who: 'man', money: 100 },
  { name: 'Vasya', who: 'man', money: 263 },
  { name: 'Kolya', who: 'man', money: 98 },
  { name: 'Katya', who: 'woman', money: 290 },
  { name: 'Olya', who: 'woman', money: 5 }
];

const whoWithMoney = groupBy(arr, 'who', 'money');
/* [
    { who: 'man', money: 461 },
    { who: 'woman', money: 295 }
  ]
*/ 

const whoNameWithMoney = groupBy(arr, 'who,name', 'money');
/* [
    { who: 'man', name: 'Vasay', money: 363 },
    { who: 'man', name: 'Kolya', money: 98 },
    { who: 'woman', name: 'Katya', money: 290 },
    { who: 'woman', name: 'Olya', money: 5 }
  ]
*/ 

Without summary props

const who = groupBy(arr, 'who');
// [{ who: 'man', who: 'woman' }]

Coercion str to num

const badArray = [
    { name: 'a', who: 'people', money: '10' },
    { name: 'b', who: 'people', money: '15' },
    { name: 'b', who: 'animals', money: '0' }
  ];

const result = groupBy(badArray, 'who', 'money');
/* [
    { who: 'people', money: 25 },
    { who: 'animals', money: 0 }
  ]
*/

Array with holes in sum props

const badArray = [
    { name: 'a', who: 'people', money: '10' },
    { name: 'b', who: 'people' },
    { name: 'b', who: 'animals', money: '0' }
];

const result = groupBy(badArray, 'who', 'money');
/* [
    { who: 'people', money: 10 },
    { who: 'animals', money: 0 }
  ]
*/

Array with holes in grouped props

const badArray = [
    { name: 'a', who: 'people', money: '10' },
    { name: 'b', money: '16' },
    { name: 'b', who: 'animals', money: '0' }
];

const result = groupBy(badArray, 'who', 'money');
/* [
    { who: 'people', money: 10 },
    { who: undefined, money: 16 },
    { who: 'animals', money: 0 }
  ]
*/

Collapse arrays by objects

const arr = [
      {
        who: { role: 'dev', access: [ 'dev' ] },
        profile: { name: 'vasya', email: '[email protected]' },
        commits: 140
      },
      {
        who: { role: 'dev', access: [ 'dev' ] },
        profile: { name: 'kolya', email: '[email protected]' },
        commits: 156
      },
      {
        who: { role: 'main', access: [ 'dev', 'master' ] },
        profile: { name: 'kolya', email: '[email protected]' },
        commits: 11
      },
      {
        who: { role: 'main', access: [ 'dev', 'master' ] },
        profile: { name: 'gg', email: '[email protected]' },
        commits: 260
      }
    ];

const result = groupBy(arr, 'profile', 'commits');
/* [
    {
      profile: { name: 'vasya', email: '[email protected]' },
      commits: 140
    },
    {
      profile: { name: 'kolya', email: '[email protected]' },
      commits: 167
    },
    {
      profile: { name: 'gg', email: '[email protected]' },
      commits: 260
    }
  ]
*/

With additional function

 const fn = (grouped) => {
   return grouped.profile.email;
 };

 const result = groupBy(arr, 'profile', 'commits', fn);
 /* [
      {  
        profile: { name: 'vasya', email: '[email protected]' },
        commits: 140
      },
      {
        profile: { name: 'kolya', email: '[email protected]' },
        commits: 167
      },
      {
        profile: { name: 'gg', email: '[email protected]' },
        commits: 260
      }
    ]
*/