array.prototype.groupby

    1.1.0 • Public • Published

    array.prototype.groupby Version Badge

    dependency status dev dependency status License Downloads

    npm badge

    An ESnext spec-compliant Array.prototype.groupBy shim/polyfill/replacement that works as far down as ES3.

    This package implements the es-shim API interface. It works in an ES3-supported environment and complies with the proposed spec.

    Because Array.prototype.groupBy depends on a receiver (the this value), the main export takes the array to operate on as the first argument.

    Getting started

    npm install --save array.prototype.groupby

    Usage/Examples

    var groupBy = require('array.prototype.groupby');
    var assert = require('assert');
    
    var arr = [0, 1, 2, 3, 4, 5];
    var parity = function (x) { return x % 2 === 0 ? 'even' : 'odd'; };
    
    var results = groupBy(arr, function (x, i, a) {
        assert.equal(x, arr[i]);
        assert.equal(a, arr);
        return parity(x);
    });
    
    assert.deepEqual(results, {
        __proto__: null,
        even: [0, 2, 4],
        odd: [1, 3, 5],
    });
    var groupBy = require('array.prototype.groupby');
    var assert = require('assert');
    /* when Array#groupBy is not present */
    delete Array.prototype.groupBy;
    var shimmed = groupBy.shim();
    
    assert.equal(shimmed, groupBy.getPolyfill());
    assert.deepEqual(arr.groupBy(parity), groupBy(arr, parity));
    var groupBy = require('array.prototype.groupby');
    var assert = require('assert');
    /* when Array#groupBy is present */
    var shimmed = groupBy.shim();
    
    assert.equal(shimmed, Array.prototype.groupBy);
    assert.deepEqual(arr.groupBy(parity), groupBy(arr, parity));

    Tests

    Simply clone the repo, npm install, and run npm test

    Install

    npm i array.prototype.groupby

    DownloadsWeekly Downloads

    616

    Version

    1.1.0

    License

    MIT

    Unpacked Size

    16.6 kB

    Total Files

    17

    Last publish

    Collaborators

    • ljharb