import { isEmpty, ifElse, pipe, filter, groupBy, toUpper, replace } from "ramda" import { TickerDisplayItemProps } from "./interface" export const toSearchByKeyword = (search: string, tabActiveQuote: string, tickerList: TickerDisplayItemProps[]) => { try { return pipe( replace(/\//g, ""), toUpper, ifElse( //条件 isEmpty, //onTrue回调 () => tickerList, //onFalse回调 keyword => pipe( // 筛选出包含关键字的 filter((v: TickerDisplayItemProps) => v.symbol.includes(keyword)), groupBy(_item => { //以某种条件(tabActiveQuote结算货币)进行group分组 const _indexOf = _item?.symbol.split(tabActiveQuote)?.[0]?.indexOf(keyword) //匹配到的关键词(TC)在symbol(BTC/USDT)中的位置(1) return _indexOf === -1 ? String(Infinity) : String(_indexOf) //如未匹配到 则将key设置为Infinity表示最靠后 }), group => { //小-》大排序 const _keys = Object.keys(group) .map(v => Number(v)) .sort((a, b) => a - b) //以上文index的排序重新组合成结果数组 const newList: TickerDisplayItemProps[] = [] for (let index = 0; index < _keys.length; index++) { const item = group[_keys[index].toString()] console.log("item", item) if (Array.isArray(item) && item.length) { newList.push(...item) } } return newList } )(tickerList) ) )(search) } catch (error) {} return [] }