From bf401483306a9d63ae45b533beb568f357a3ad43 Mon Sep 17 00:00:00 2001 From: Radoslav Karaivanov Date: Mon, 28 Jul 2025 18:17:02 +0300 Subject: [PATCH] refactor(combo): Use native groupby implementation --- src/components/combo/operations/group.ts | 57 ++++++++++++------------ src/components/common/util.ts | 18 -------- tsconfig.json | 2 +- 3 files changed, 30 insertions(+), 47 deletions(-) diff --git a/src/components/combo/operations/group.ts b/src/components/combo/operations/group.ts index ee107dd08..d5b54f8ec 100644 --- a/src/components/combo/operations/group.ts +++ b/src/components/combo/operations/group.ts @@ -1,45 +1,46 @@ -import { groupBy } from '../../common/util.js'; import type { DataController } from '../controllers/data.js'; import type { ComboRecord, Keys } from '../types.js'; -export default class GroupDataOperation { - protected orderBy = new Map( - Object.entries({ - asc: 1, - desc: -1, - }) - ); +const OrderBy = Object.freeze({ asc: 1, desc: -1 }); - public apply(data: ComboRecord[], controller: DataController) { +export default class GroupDataOperation { + public apply( + data: ComboRecord[], + controller: DataController + ): ComboRecord[] { const { groupingOptions: { groupKey, valueKey, displayKey, direction }, } = controller; - if (!groupKey) return data; + if (!groupKey) { + return data; + } - const groups = Object.entries( - groupBy(data, (item) => item.value[groupKey] ?? 'Other') + const grouped = Map.groupBy( + data, + (item) => (item.value[groupKey] as string) ?? 'Other' ); + const keys = Array.from(grouped.keys()); + if (direction !== 'none') { - const orderBy = this.orderBy.get(direction); - groups.sort((a, b) => { - return orderBy! * controller.compareCollator.compare(a[0], b[0]); - }); + const orderBy = OrderBy[direction]; + keys.sort((a, b) => orderBy * controller.compareCollator.compare(a, b)); } - return groups.flatMap(([group, items]) => { - items.unshift({ - dataIndex: -1, - header: true, - value: { - [valueKey as Keys]: group, - [displayKey as Keys]: group, - [groupKey as Keys]: group, - } as T, - }); - - return items; + return keys.flatMap((key) => { + return [ + { + value: { + [valueKey as Keys]: key, + [displayKey as Keys]: key, + [groupKey as Keys]: key, + } as T, + header: true, + dataIndex: -1, + }, + ...grouped.get(key)!, + ]; }); } } diff --git a/src/components/common/util.ts b/src/components/common/util.ts index b39e987b5..e0523cb4e 100644 --- a/src/components/common/util.ts +++ b/src/components/common/util.ts @@ -167,24 +167,6 @@ export function findElementFromEventPath( return getElementsFromEventPath(event).find(func) as T | undefined; } -export function groupBy(array: T[], key: keyof T | ((item: T) => any)) { - const result: Record = {}; - const _get = isFunction(key) ? key : (item: T) => item[key]; - - for (const item of array) { - const category = _get(item); - const group = result[category]; - - if (Array.isArray(group)) { - group.push(item); - } else { - result[category] = [item]; - } - } - - return result; -} - export function first(arr: T[]) { return arr.at(0) as T; } diff --git a/tsconfig.json b/tsconfig.json index b05d68243..bc9b8a1a3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "es2021", "module": "es2020", - "lib": ["es2023", "DOM", "DOM.Iterable"], + "lib": ["ESNext", "DOM", "DOM.Iterable"], "outDir": "dist", "rootDir": "./", "declaration": true,