@@ -5,19 +5,19 @@ A proposal to make grouping of items in an array easier.
55``` js
66const array = [1 , 2 , 3 , 4 , 5 ];
77
8- // groupBy groups items by arbitrary key.
8+ // group groups items by arbitrary key.
99// In this case, we're grouping by even/odd keys
10- array .groupBy ((num , index , array ) => {
10+ array .group ((num , index , array ) => {
1111 return num % 2 === 0 ? ' even' : ' odd' ;
1212});
1313
1414// => { odd: [1, 3, 5], even: [2, 4] }
1515
16- // groupByToMap returns items in a Map, and is useful for grouping using
16+ // groupToMap returns items in a Map, and is useful for grouping using
1717// an object key.
1818const odd = { odd: true };
1919const even = { even: true };
20- array .groupByToMap ((num , index , array ) => {
20+ array .groupToMap ((num , index , array ) => {
2121 return num % 2 === 0 ? even: odd;
2222});
2323
@@ -40,12 +40,27 @@ thought of map-group-reduce). The ability to combine like data into
4040groups allows developers to compute higher order datasets, like the
4141average age of a cohort or daily LCP values for a webpage.
4242
43- Two methods are offered, ` groupBy ` and ` groupByToMap ` . The first returns a
43+ Two methods are offered, ` group ` and ` groupToMap ` . The first returns a
4444null-prototype object, which allows ergonomic destructuring and prevents
4545accidental collisions with global Object properties. The second returns
4646a regular ` Map ` instance, which allows grouping on complex key types
4747(imagine a compound key or [ tuple] ).
4848
49+ ## Why ` group ` and not ` groupBy ` ?
50+
51+ We've [ found] [ sugar-bug ] a web compatibility issue with the name
52+ ` groupBy ` . The [ Sugar] [ sugar ] library until v1.4.0 conditionally
53+ monkey-patches ` Array.prototype ` with an incompatible method. By
54+ providing a native ` groupBy ` , these versions of Sugar would fail to
55+ install their implementation, and any sites that depend on their
56+ behavior would break. We've found some 660 origins that use these
57+ versions of the Sugar library.
58+
59+ In a similar situation, we renamed the ` Array.prototype.flatten `
60+ proposal to ` flat ` . Note that we did not rename it to ` flattened `
61+ (though it was considered). We've taken the same approach, renaming
62+ ` groupBy ` to ` group ` .
63+
4964## Polyfill
5065
5166- A polyfill is available in the [ core-js] library. You can find it in the [ ECMAScript proposals section] [ core-js-section ] .
@@ -60,3 +75,5 @@ a regular `Map` instance, which allows grouping on complex key types
6075[ core-js-section ] : https://github.com/zloirock/core-js#array-grouping
6176[ lodash ] : https://lodash.com/docs/4.17.15#groupBy
6277[ lodash-npm ] : https://www.npmjs.com/package/lodash.groupby
78+ [ sugar ] : https://sugarjs.com/
79+ [ sugar-bug ] : https://github.com/tc39/proposal-array-grouping/issues/37
0 commit comments