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

array-grouping-polyfill

v0.0.3

Published

proposal-array-grouping polyfill. Polyfill for array.prototype.groupBy and array.prototype.groupByMap

Downloads

30

Readme

Polyfill for tc39/proposal-array-grouping

Simple polyfill for tc39/proposal-array-grouping, if you dont feel like including core-js

How to use

import 'array-grouping-polyfill'


const array = [-1, 2, -4, 5, 'foo', 0, 6, 3, -3, 5];
const {negative, positive, zero, nan} = array.groupBy((i) => {
    switch(true) {
        case typeof i !== 'number':
            return 'nan';
        case i < 0:
            return 'negative'
        case i > 0:
            return 'positive'
        default:
            return 'zero';
    }
});


console.log(negative) // -1, -4, -3
console.log(positive) // 2, 5, 6, 3, 5
console.log(zero) // 0
console.log(nan) // foo

proposal-array-grouping

(Note this is a copy from tc39/proposal-array-grouping)

A proposal to make grouping of items in an array easier.

const array = [1, 2, 3, 4, 5];

// groupBy groups items by arbitrary key.
// In this case, we're grouping by even/odd keys
array.groupBy((num, index, array) => {
  return num % 2 === 0 ? 'even': 'odd';
});

// =>  { odd: [1, 3, 5], even: [2, 4] }

// groupByToMap returns items in a Map, and is useful for grouping using
// an object key.
const odd  = { odd: true };
const even = { even: true };
array.groupByToMap((num, index, array) => {
  return num % 2 === 0 ? even: odd;
});

// =>  Map { {odd: true}: [1, 3, 5], {even: true}: [2, 4] }

Array grouping is an extremely common operation, best exemplified by SQL's GROUP BY clause and MapReduce programming (which is better thought of map-group-reduce). The ability to combine like data into groups allows developers to compute higher order datasets, like the average age of a cohort or daily LCP values for a webpage.

Two methods are offered, groupBy and groupByToMap. The first returns a null-prototype object, which allows ergonomic destructuring and prevents accidental collisions with global Object properties. The second returns a regular Map instance, which allows grouping on complex key types (imagine a compound key or [tuple]).

Transpiled standalone code

Just want to use a small blob of code that is stand alone?

void (function() {
    var _a, _b;
    var _c, _d;
    (_a = (_c = Array.prototype).groupBy) !== null && _a !== void 0 ? _a : (_c.groupBy = function (callback, thisArg) {
        var obj = {};
        this.forEach(function (value, idx, self) {
            var _a;
            var ret = thisArg ? callback.call(thisArg, value, idx, self) : callback(value, idx, self);
            ((_a = obj[ret]) !== null && _a !== void 0 ? _a : (obj[ret] = [])).push(value);
        });
        return obj;
    });
    (_b = (_d = Array.prototype).groupByToMap) !== null && _b !== void 0 ? _b : (_d.groupByToMap = function (callback, thisArg) {
        var map = new Map();
        this.forEach(function (value, idx, self) {
            var ret = thisArg ? callback.call(thisArg, value, idx, self) : callback(value, idx, self);
            var group = map.get(ret) || [];
            if (group.push(value) === 1)
                map.set(ret, group);
        });
        return map;
    });
})()