Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

group and groupToMap why not merge into one api #43

Closed
c0dedance opened this issue Aug 28, 2022 · 6 comments
Closed

group and groupToMap why not merge into one api #43

c0dedance opened this issue Aug 28, 2022 · 6 comments

Comments

@c0dedance
Copy link

c0dedance commented Aug 28, 2022

group and groupToMap why not merge into one api, I think the implementation of the latter includes the former

@ljharb
Copy link
Member

ljharb commented Aug 28, 2022

How would you specify that you wanted an object vs a Map, if it was in a single API?

@jimmywarting

This comment was marked as resolved.

@Andrew-Cottrell
Copy link

Andrew-Cottrell commented Sep 5, 2022

I'm questioning weather or not groupToMap is really necessary... it seems pretty easy to just use slice if you need a new copy...

[].slice().group(...)

group(...) evaluates to a plain object rather than a Map (see examples).

slice() is unnecessary here as group and groupToMap do not modify the original array.

@michaelficarra
Copy link
Member

I guess in theory we could have a single API which produces "entries" (an array of pairs) and then that could be passed to either Object.fromEntries or new Map (or maybe others) depending on the desired use.

@zloirock
Copy link
Contributor

zloirock commented Sep 13, 2022

@michaelficarra how do you propose to handle keys?

Object.keys(['1', 1, { toString() { return 1 } }].group(it => it)).length; // => 1
['1', 1, { toString() { return 1 } }].groupToMap(it => it).size; // => 3

@bakkot
Copy link
Contributor

bakkot commented Sep 13, 2022

This has already been discussed at some length in #3. For the entries suggestion, see in particular this comment and the few preceding it: you can't produce entries without know if you are producing entries-as-for-object or entries-as-for-Map, because you don't know if "1" and 1 should be treated as being the same group.

If you want entries, make a Map and iterate it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants