| [9af201e] | 1 | # object.groupby <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
|---|
| 2 |
|
|---|
| 3 | [![github actions][actions-image]][actions-url]
|
|---|
| 4 | [![coverage][codecov-image]][codecov-url]
|
|---|
| 5 | [![License][license-image]][license-url]
|
|---|
| 6 | [![Downloads][downloads-image]][downloads-url]
|
|---|
| 7 |
|
|---|
| 8 | [![npm badge][npm-badge-png]][package-url]
|
|---|
| 9 |
|
|---|
| 10 | An ESnext spec-compliant `Object.groupBy` shim/polyfill/replacement that works as far down as ES3.
|
|---|
| 11 |
|
|---|
| 12 | This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the proposed [spec](https://tc39.github.io/proposal-array-grouping/).
|
|---|
| 13 |
|
|---|
| 14 | ## Getting started
|
|---|
| 15 |
|
|---|
| 16 | ```sh
|
|---|
| 17 | npm install --save object.groupby
|
|---|
| 18 | ```
|
|---|
| 19 |
|
|---|
| 20 | ## Usage/Examples
|
|---|
| 21 |
|
|---|
| 22 | ```js
|
|---|
| 23 | var groupBy = require('object.groupby');
|
|---|
| 24 | var assert = require('assert');
|
|---|
| 25 |
|
|---|
| 26 | var arr = [0, 1, 2, 3, 4, 5];
|
|---|
| 27 | var parity = function (x) { return x % 2 === 0 ? 'even' : 'odd'; };
|
|---|
| 28 |
|
|---|
| 29 | var results = groupBy(arr, function (x, i) {
|
|---|
| 30 | assert.equal(x, arr[i]);
|
|---|
| 31 | return parity(x);
|
|---|
| 32 | });
|
|---|
| 33 |
|
|---|
| 34 | assert.deepEqual(results, {
|
|---|
| 35 | __proto__: null,
|
|---|
| 36 | even: [0, 2, 4],
|
|---|
| 37 | odd: [1, 3, 5],
|
|---|
| 38 | });
|
|---|
| 39 | ```
|
|---|
| 40 |
|
|---|
| 41 | ```js
|
|---|
| 42 | var groupBy = require('object.groupby');
|
|---|
| 43 | var assert = require('assert');
|
|---|
| 44 | /* when Object.groupBy is not present */
|
|---|
| 45 | delete Object.groupBy;
|
|---|
| 46 | var shimmed = groupBy.shim();
|
|---|
| 47 |
|
|---|
| 48 | assert.equal(shimmed, groupBy.getPolyfill());
|
|---|
| 49 | assert.deepEqual(Object.groupBy(arr, parity), groupBy(arr, parity));
|
|---|
| 50 | ```
|
|---|
| 51 |
|
|---|
| 52 | ```js
|
|---|
| 53 | var groupBy = require('object.groupby');
|
|---|
| 54 | var assert = require('assert');
|
|---|
| 55 | /* when Array#group is present */
|
|---|
| 56 | var shimmed = groupBy.shim();
|
|---|
| 57 |
|
|---|
| 58 | assert.equal(shimmed, Object.groupBy);
|
|---|
| 59 | assert.deepEqual(Object.groupBy(arr, parity), groupBy(arr, parity));
|
|---|
| 60 | ```
|
|---|
| 61 |
|
|---|
| 62 | ## Tests
|
|---|
| 63 | Simply clone the repo, `npm install`, and run `npm test`
|
|---|
| 64 |
|
|---|
| 65 | [package-url]: https://npmjs.org/package/object.groupby
|
|---|
| 66 | [npm-version-svg]: https://versionbadg.es/es-shims/Object.groupBy.svg
|
|---|
| 67 | [deps-svg]: https://david-dm.org/es-shims/Object.groupBy.svg
|
|---|
| 68 | [deps-url]: https://david-dm.org/es-shims/Object.groupBy
|
|---|
| 69 | [dev-deps-svg]: https://david-dm.org/es-shims/Object.groupBy/dev-status.svg
|
|---|
| 70 | [dev-deps-url]: https://david-dm.org/es-shims/Object.groupBy#info=devDependencies
|
|---|
| 71 | [npm-badge-png]: https://nodei.co/npm/object.groupby.png?downloads=true&stars=true
|
|---|
| 72 | [license-image]: https://img.shields.io/npm/l/object.groupby.svg
|
|---|
| 73 | [license-url]: LICENSE
|
|---|
| 74 | [downloads-image]: https://img.shields.io/npm/dm/object.groupby.svg
|
|---|
| 75 | [downloads-url]: https://npm-stat.com/charts.html?package=object.groupby
|
|---|
| 76 | [codecov-image]: https://codecov.io/gh/es-shims/Object.groupBy/branch/main/graphs/badge.svg
|
|---|
| 77 | [codecov-url]: https://app.codecov.io/gh/es-shims/Object.groupBy/
|
|---|
| 78 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/Object.groupBy
|
|---|
| 79 | [actions-url]: https://github.com/es-shims/Object.groupBy/actions
|
|---|