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

Hotfix LO groupBy #2517

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/swapv2/LimitOrder/OrderBook/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { formatDisplayNumber } from 'utils/numbers'

import RefreshLoading from '../ListLimitOrder/RefreshLoading'
import { NoResultWrapper } from '../ListOrder'
import { groupToMap } from '../helpers'
import { LimitOrderFromTokenPair, LimitOrderFromTokenPairFormatted } from '../type'
import OrderItem from './OrderItem'
import TableHeader from './TableHeader'
Expand Down Expand Up @@ -130,7 +131,7 @@ const formatOrders = (

// Merge orders with the same rate
const mergedOrders: LimitOrderFromTokenPairFormatted[] = []
const groupOrders = Map.groupBy(ordersFormatted, ({ rate }: LimitOrderFromTokenPairFormatted) => rate)
const groupOrders = groupToMap(ordersFormatted, ({ rate }: LimitOrderFromTokenPairFormatted) => rate)

groupOrders.forEach((group: LimitOrderFromTokenPairFormatted[]) => {
const mergedOrder = group?.reduce(
Expand Down
10 changes: 10 additions & 0 deletions src/components/swapv2/LimitOrder/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,13 @@ export const getPayloadTracking = (order: LimitOrder, networkName: string, paylo
order_id: id,
}
}

export const groupToMap = <K, T>(items: Iterable<T>, keySelector: (item: T, index?: number) => K): Map<K, T[]> => {
return [...items].reduce((accumulator: Map<K, T[]>, currentValue: T) => {
const newValue = accumulator.get(keySelector(currentValue)) || []
newValue.push(currentValue)
accumulator.set(keySelector(currentValue), newValue)

return accumulator
}, new Map())
}
19 changes: 0 additions & 19 deletions src/types/object.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,4 @@ interface ObjectConstructor {
* @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.
*/
keys<T extends string>(o: { [keys in T]: unknown }): T[]

/**
* Groups members of an iterable according to the return value of the passed callback.
* @param items An iterable.
* @param keySelector A callback which will be invoked for each item in items.
*/
groupBy<K extends PropertyKey, T>(
items: Iterable<T>,
keySelector: (item: T, index: number) => K,
): Partial<Record<K, T[]>>
}

interface MapConstructor {
/**
* Groups members of an iterable according to the return value of the passed callback.
* @param items An iterable.
* @param keySelector A callback which will be invoked for each item in items.
*/
groupBy<K, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K): Map<K, T[]>
}
Loading