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

fix(groupBy): preserve Object prototype method #902

Merged
merged 3 commits into from
Dec 15, 2024

Conversation

SaeWooKKang
Copy link
Contributor

What this PR solves

  • Fix the groupBy function to properly preserve Object prototype (like toString, valueOf).

Motivation

While implementing "fix(groupBy): Support keys like toString or valueOf in groupBy", we noticed that prototype methods were removed entirely. This is likely unintended behavior unless the user specifically uses these method names as keys.

For example, even when not using 'toString' as a grouping key, attempting to convert the result object to a string would fail:

const data = [{ category: 'fruit', name: 'apple' }];
const result = groupBy(data, item => item.category);

// This throws TypeError: Cannot convert object to primitive value
String(result);

Copy link

vercel bot commented Dec 14, 2024

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 Dec 15, 2024 0:41am

@raon0211
Copy link
Collaborator

Hello, thanks for helping us!

The design principle for es-toolkit is to support everyday usecase, but skip edge cases. (You can refer to our contributing docs).

Is there an actual usecase for the behavior you described? I'm asking because I thought we don't usually access prototype methods when using groupBy.

@SaeWooKKang
Copy link
Contributor Author

@raon0211
Thank you for the quick feedback.

As you mentioned, there are no direct uses of prototype methods in my code. However, I encountered an issue in a situation beyond my direct control:

  1. Created a React boiler project using npm create vite
  2. Started the development server with npm run dev
  3. Processed server responses using groupBy
  4. Error occurred when trying to verify the results with console.log

While the build output works correctly in production, errors occur in the development environment.

This is clearly a situation that developers cannot directly control, and I believe it can be quite tricky to debug.

From my perspective, it feels unnatural for an object to lack its prototype methods when its type is clearly an object - similar to how it would be strange for an array to lack its prototype methods.

However, I understand that this case could be considered an edge case according to es-toolkit's design principles, and I respect your perspective on this matter. Thank you for your feedback.

@raon0211
Copy link
Collaborator

Hi, thanks for your help! I see what you pointed out.

While reviewing other functions, I noticed that many of them, like countBy and zipObject, use plain objects ({}) to build their results. To keep things consistent, I updated the code to also use {} for the result.

However, I wasn’t sure if this is something we need to enforce with test cases, so I removed that part.

Copy link
Collaborator

@raon0211 raon0211 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@raon0211 raon0211 merged commit 0f98ca7 into toss:main Dec 15, 2024
5 of 7 checks passed
@SaeWooKKang
Copy link
Contributor Author

Thank you for understanding.

However, I wasn't sure if this is something we need to enforce with test cases, so I removed that part.

I agree with this point.

The commit caused existing tests to fail. Another person has submitted a #906 to address this issue.

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

Successfully merging this pull request may close these issues.

2 participants