/Array.prototype.groupToMap

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

Primary LanguageJavaScriptMIT LicenseMIT

array.prototype.groupToMap Version Badge

dependency status dev dependency status License Downloads

npm badge

An ESnext spec-compliant Array.prototype.groupToMap 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.groupToMap 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.grouptomap

Usage/Examples

var groupToMap = require('array.prototype.grouptomap');
var assert = require('assert');

var arr = [0, 1, 2, 3, 4, 5];
var parity = function (x) { return x % 2 === 0 ? 'even' : 'odd'; };

var results = groupToMap(arr, function (x, i, a) {
    assert.equal(x, arr[i]);
    assert.equal(a, arr);
    return parity(x);
});

assert.deepEqual(results, new Map([
    ['even', [0, 2, 4]],
    ['odd', [1, 3, 5]],
]));
var groupToMap = require('array.prototype.grouptomap');
var assert = require('assert');
/* when Array#groupToMap is not present */
delete Array.prototype.groupToMap;
var shimmed = groupToMap.shim();

assert.equal(shimmed, groupToMap.getPolyfill());
assert.deepEqual(arr.groupToMap(parity), groupToMap(arr, parity));
var groupToMap = require('array.prototype.grouptomap');
var assert = require('assert');
/* when Array#groupToMap is present */
var shimmed = groupToMap.shim();

assert.equal(shimmed, Array.prototype.groupToMap);
assert.deepEqual(arr.groupToMap(parity), groupToMap(arr, parity));

Tests

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