es-toolkit
es-toolkit copied to clipboard
fix(compat/orderby): add missed test cases and fix the problematic parts
Description
compat/orderby missed including these testcases(https://github.com/lodash/lodash/blob/6a2cc1dfcf7634fea70d1bc5bd22db453df67b42/test/sortBy-methods.spec.js) and hidden case(only implemented). And it also missed some of feature, so I added.
! Important
Currently, we had a bug that makes by below code:
// src/compat/array/orderBy.ts:81
keys = keys.map(key => getPath(key, collection[0]));
Before the changes, it checked the key is not a deep path only with the first element of the collection. So if the first element is different with others, then throw the TypeError like this:
orderBy([{ a: 1 }, { 'a.b': 3 }, { 'a.b': 1 }], ['a.b'], ['asc']) // TypeError: Cannot read properties of undefined (reading 'b')
orderBy([{ 'a.b': 3 }, { a: 1 }, { 'a.b': 1 }], ['a.b'], ['asc']) // success
So I changed the logic of checking keys similar with lodash(slower but safer).
Benchmarks
The performance result is changed, you can check result by a comment.
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 29, 2024 2:05am |
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 99.77%. Comparing base (
b9966aa) to head (8258b85).
Additional details and impacted files
@@ Coverage Diff @@
## main #427 +/- ##
=======================================
Coverage 99.77% 99.77%
=======================================
Files 165 165
Lines 1325 1334 +9
Branches 357 360 +3
=======================================
+ Hits 1322 1331 +9
Misses 2 2
Partials 1 1
I Improved performance by preparing all cases of criterion before comparing.
At now, we don't need to use isKey (heavy function) each criterion.
N: length of collection, K: length of criteria O(NK) -> 0
And also we convert deep path to nested path at once. Before code converted each collection items.
N: length of collection, K: length of criteria O(NK) -> O(K)
Before
After
Thanks for your great work!
After merging this PR, I am going to implement compat/sortBy based on compat/orderBy!