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

feat(orderBy): add handling one is a string and the other is a number case #365

Merged
merged 7 commits into from
Aug 10, 2024

Conversation

dayongkr
Copy link
Contributor

@dayongkr dayongkr commented Aug 9, 2024

Description

If value a is a 'hello' and value b is a 24, then value a will be converted to NaN. So, a > b and a < b will be false in this case.

Because Javascript converts to number, when types of the compared values are not same.

So in this update, I enhanced the compareValues function to handle cases where the types of a and b differ between strings and numbers.

Handling the other cases like object or array type is not good for our simplicity, So I just return 0 in these cases.

The function now converts number to string when necessary to ensure they can be compared correctly without causing NaN errors.

Specifically, if a is a string and b is a number (or vice versa), the number is converted to a string before comparison.

This adjustment prevents issues that might arise when comparing mixed types and ensures consistent behavior when sorting or ordering data.

If this enhancement is acceptable, then I will add this description in a document.

Example

const data = [
  { id: 1, value: 'a' },
  { id: 2, value: 2 },
  { id: 12, value: 1 },
  { id: 5, value: 'b' },
  { id: 4, value: 2 },
  { id: 43, value: 'c' },
  { id: 24, value: 3 },
  { id: 3, value: '3a' },
  { id: 6, value: '2a' },
  { id: 7, value: '1cs' },
];

console.log(orderBy(data, ['value', 'id'], ['asc', 'asc']))
/*
[
  { id: 12, value: 1 },
  { id: 7, value: '1cs' },
  { id: 2, value: 2 },
  { id: 4, value: 2 },
  { id: 6, value: '2a' },
  { id: 24, value: 3 },
  { id: 3, value: '3a' },
  { id: 1, value: 'a' },
  { id: 5, value: 'b' },
  { id: 43, value: 'c' },
]
*/

close #361

@dayongkr dayongkr requested a review from raon0211 as a code owner August 9, 2024 10:12
Copy link

vercel bot commented Aug 9, 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 Aug 10, 2024 7:11am

@codecov-commenter
Copy link

codecov-commenter commented Aug 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.82%. Comparing base (bb94b88) to head (ff52173).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #365   +/-   ##
=======================================
  Coverage   99.82%   99.82%           
=======================================
  Files         142      143    +1     
  Lines        1116     1124    +8     
  Branches      299      302    +3     
=======================================
+ Hits         1114     1122    +8     
  Misses          2        2           

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 4af0662 into toss:main Aug 10, 2024
8 checks passed
raon0211 added a commit that referenced this pull request Aug 10, 2024
Revert "feat(orderBy): add handling one is a string and the other is a number case (#365)"

This reverts commit 4af0662.
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.

Unexpected behavior using orderBy
3 participants