Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Array.prototype.groupBy[toMap] to JS lib definitions. #47171

Open
Pokute opened this issue Dec 16, 2021 · 4 comments
Open

Add Array.prototype.groupBy[toMap] to JS lib definitions. #47171

Pokute opened this issue Dec 16, 2021 · 4 comments

Comments

@Pokute
Copy link

@Pokute Pokute commented Dec 16, 2021

The proposal for Array.prototype.groupBy and Array.prototype.groupByToMap reached stage 3 recently (although the proposal page has not been updated from stage 2).

Search terms: proposal-array-grouping array groupBy ernest

lib Update Request

With advancement into stage 3, it's time for JS engines, transpilers and toolings to start implementing support for new features and provide feedback for the proposal's advancement into stage 4.

Configuration Check

This is so new that it needs to go into ESNext target.

Missing / Incorrect Definition

Missing Array.prototype.groupBy and Array.prototype.groupByToMap.

Sample Code

const array = [1, 2, 3, 4, 5];

// groupBy groups items by arbitrary key.
// In this case, we're grouping by even/odd keys
array.groupBy((num, index, array) => {
  return num % 2 === 0 ? 'even': 'odd';
});

// =>  { odd: [1, 3, 5], even: [2, 4] }

// groupByToMap returns items in a Map, and is useful for grouping using
// an object key.
const odd  = { odd: true };
const even = { even: true };
array.groupByToMap((num, index, array) => {
  return num % 2 === 0 ? even: odd;
});

// =>  Map { {odd: true}: [1, 3, 5], {even: true}: [2, 4] }

Sample code from https://github.com/es-shims/Array.prototype.groupBy

Documentation Link

Proposal: proposal-array-grouping

Listing of proposal-array-grouping as stage 3

Shim implementation that can the code can be tested against: es-shims/Array.prototype.groupBy

@orta
Copy link
Member

@orta orta commented Dec 16, 2021

Open for someone to take a look. I'm not quite sure off the bat what this should look like FWIW.

I think we probably need to make the same assumptions as we do with Object.entries where we can't use keyof #38520

Loading

@orta
Copy link
Member

@orta orta commented Dec 16, 2021

I asked, and we generally think this might be ok to have the more accurate typing accurately reflecting the source object. Will need a bit of testing out when there's a PR

Loading

@DanielRosenwasser
Copy link
Member

@DanielRosenwasser DanielRosenwasser commented Dec 16, 2021

Yeah, I think we could think of this as closer to fromEntries; however, fromEntries doesn't try to preserve keys at all (and I don't think it even (cleanly) could).

Loading

@DanielRosenwasser
Copy link
Member

@DanielRosenwasser DanielRosenwasser commented Dec 16, 2021

I feel like there is something close you can get with reversed mapped types - we discussed this in a recent design meeting about correlated unions.

#47109 for some description of this.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants