-
Notifications
You must be signed in to change notification settings - Fork 12
fix: token sorting #262
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
base: main
Are you sure you want to change the base?
fix: token sorting #262
Conversation
const [nativeTokens, tokensWithCurrency, tokensWithoutCurrency] = | ||
tokens.reduce< | ||
[ | ||
DisplayToken<TokenWithBalance & { type: TokenType.NATIVE }>[], | ||
DisplayToken<TokenWithBalance & { balanceInCurrency: number }>[], | ||
DisplayToken<TokenWithBalance>[], | ||
] | ||
>( | ||
([natives, withCurrencyValue, withoutCurrencyValue], displayToken) => { | ||
if (isNativeToken(displayToken)) { | ||
return [ | ||
[...natives, displayToken], | ||
withCurrencyValue, | ||
withoutCurrencyValue, | ||
]; | ||
} | ||
|
||
if (hasCurrencyValue(displayToken)) { | ||
return [ | ||
natives, | ||
[...withCurrencyValue, displayToken], | ||
withoutCurrencyValue, | ||
]; | ||
} | ||
|
||
return [ | ||
natives, | ||
withCurrencyValue, | ||
[...withoutCurrencyValue, displayToken], | ||
]; | ||
}, | ||
[[], [], []], | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider to use lodash groupby method instead
https://lodash.com/docs#groupBy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, have you tried to use Lodash's orderBy
function?
It should be much cleaner
return [ | ||
[...natives, displayToken], | ||
withCurrencyValue, | ||
withoutCurrencyValue, | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't return a new accumulator value, because it litters the memory like crazy.
Original accumulator should be mutated instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tokens.reduce(acc, token => {
const [natives, withCurrentValue, withoutCurrentValue] = acc;
if (isNativeToken(token)) {
natives.push(token)
}
....
return acc
})
Changes
Testing
Verify tokens are sorted as expected: native token first, then the highest fiat value, then highest balance (if fiat value is unknown), then alphabetically.
Screenshots:
Checklist for the author