toss / es-toolkit

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.
https://es-toolkit.slash.page
Other
6.25k stars 263 forks source link

feat(groupConsecutiveBy): Implement a new function on array to chunk consecutive elements with same key into groups #434

Closed lqhuang closed 1 week ago

lqhuang commented 2 weeks ago

Hi there,

Thanks for such amazing lib. I'm trying to transform consecutive discriminated union elements have the same key in an array into a new array with grouped unions, but I only found groupBy function.

Hence, here I want to propose a new function on array toolkits to transform things I described above.

const array = [
  { category: 'fruit', name: 'apple' },
  { category: 'fruit', name: 'banana' },
  { category: 'vegetable', name: 'carrot' }
  { category: 'vegetable', name: 'tomato' }
  { category: 'fruit', name: 'orange' }
];
const result = groupConsecutiveBy(array, item => item.category);
// result will be:
// [
//   [
//     { category: 'fruit', name: 'apple' },
//     { category: 'fruit', name: 'banana' }
//   ],
//   [
//     { category: 'vegetable', name: 'carrot' }
//     { category: 'vegetable', name: 'tomato' }
//   ],
//   [
//     { category: 'fruit', name: 'orange' }
//   ]
// ]

You could see the details in src changes. And there are some notable comments

It will be greatly appreciated if anyone provide suggestions to improve it!

Regards

vercel[bot] commented 2 weeks ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
es-toolkit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 26, 2024 2:39pm
raon0211 commented 2 weeks ago

Hello :) Thanks for the suggestion! I see the need for this function, but I'm still not sure how frequently it will be used by our users. We might consider defining it in each repository where it's needed, but not in es-toolkit. What do you think?

lqhuang commented 2 weeks ago

@raon0211 Thanks for reply! Never mind, it's fine to close. I totally understand this function doesn't include in loadash either. I just thought es-toolkit could have more usage cases than lodash.

Feel free to close 😄, or just keep it open until other people show interesting on this feature or provide more feedbacks.

raon0211 commented 1 week ago

Thanks for your understanding! :)