Skip to content

Commit

Permalink
Hotfix LO groupBy (#2517)
Browse files Browse the repository at this point in the history
* remove Map.groupBy by self-implement func

* re-add object type

---------

Co-authored-by: Tien Nguyen <tiennguyen@Nguyens-MacBook-Pro-4.local>
  • Loading branch information
tienkane and Tien Nguyen authored Aug 16, 2024
1 parent e19e35e commit c3f3830
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
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[]>
}

0 comments on commit c3f3830

Please sign in to comment.