Skip to content

es-shims/Array.prototype.group

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

array.prototype.group Version Badge

dependency status dev dependency status License Downloads

npm badge

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

Usage/Examples

var group = require('array.prototype.group');
var assert = require('assert');

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

var results = group(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 group = require('array.prototype.group');
var assert = require('assert');
/* when Array#group is not present */
delete Array.prototype.group;
var shimmed = group.shim();

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

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

Tests

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

About

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

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks