Closed ssi02014 closed 1 week 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 | Sep 4, 2024 1:05am |
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 99.78%. Comparing base (
cb33fd2
) to head (8ef84dc
). Report is 1 commits behind head on main.
@raon0211
As a footnote to that function, the return type of flatMap is Array<FlatArray<U[], D>>
, so specifying the type as infinity
will not result in the intended type inference.
Therefore, we believe that flatMapDeep
with the intended type inference is useful and serves the purpose of compatibility with lodash
.
const arr = [1];
const newArr1 = flatMapDeep(arr, item => [[[item, item, [[item]]]]]); // number[]
newArr1.map(item => {
item; // number
item.toFixed(1); // ✅
});
const newArr2 = flatMap(arr, item => [[[item, item, [[item]]]]], Infinity);
// FlatArray<(number | number[][])[][][], 0 | 1 | -1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[]
newArr2.map(item => {
item; // FlatArray<(number | number[][])[][][], 0 | 1 | 2 | 3 | 4 | -1 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>
item.toFixed(1); // ❌
(item as number).toFixed(1); // ✅ But this is not comfortable....
});
The issue with the Infinity
type in typescript was previously mentioned by @raon0211 himself. https://github.com/toss/es-toolkit/issues/122#issuecomment-2214201240
Of course, we could work around it with something like flatMap(arr, 9999)
, but I think it's better to provide flatMapDeep
than to do that.
const newArr3 = flatMap(arr, item => [[[item, item, [[item]]]]], 9999);
// ..🤔 I don't think this is appropriate.
newArr3.map(item => {
item; // number
item.toFixed(1); // ✅
});
In fact, the performance difference between the two functions is not significant.
Yes, you're right. I accidentally forgot what we discussed 😅
cloesd: #347
Add the flatMapDeep discussed in the issue above.
benchMark
This is more performant than utilizing lodash or the built-in array functions.