Skip to content

Commit 7962650

Browse files
rkaraivanovCopilot
andauthored
refactor(combo): Use native groupby implementation (#1802)
--------- Co-authored-by: Copilot <[email protected]>
1 parent d4af765 commit 7962650

File tree

3 files changed

+30
-47
lines changed

3 files changed

+30
-47
lines changed
Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
1-
import { groupBy } from '../../common/util.js';
21
import type { DataController } from '../controllers/data.js';
32
import type { ComboRecord, Keys } from '../types.js';
43

5-
export default class GroupDataOperation<T extends object> {
6-
protected orderBy = new Map(
7-
Object.entries({
8-
asc: 1,
9-
desc: -1,
10-
})
11-
);
4+
const OrderBy = Object.freeze({ asc: 1, desc: -1 });
125

13-
public apply(data: ComboRecord<T>[], controller: DataController<T>) {
6+
export default class GroupDataOperation<T extends object> {
7+
public apply(
8+
data: ComboRecord<T>[],
9+
controller: DataController<T>
10+
): ComboRecord<T>[] {
1411
const {
1512
groupingOptions: { groupKey, valueKey, displayKey, direction },
1613
} = controller;
1714

18-
if (!groupKey) return data;
15+
if (!groupKey) {
16+
return data;
17+
}
1918

20-
const groups = Object.entries(
21-
groupBy(data, (item) => item.value[groupKey] ?? 'Other')
19+
const grouped = Map.groupBy(
20+
data,
21+
(item) => (item.value[groupKey] as string) ?? 'Other'
2222
);
2323

24+
const keys = Array.from(grouped.keys());
25+
2426
if (direction !== 'none') {
25-
const orderBy = this.orderBy.get(direction);
26-
groups.sort((a, b) => {
27-
return orderBy! * controller.compareCollator.compare(a[0], b[0]);
28-
});
27+
const orderBy = OrderBy[direction];
28+
keys.sort((a, b) => orderBy * controller.compareCollator.compare(a, b));
2929
}
3030

31-
return groups.flatMap(([group, items]) => {
32-
items.unshift({
33-
dataIndex: -1,
34-
header: true,
35-
value: {
36-
[valueKey as Keys<T>]: group,
37-
[displayKey as Keys<T>]: group,
38-
[groupKey as Keys<T>]: group,
39-
} as T,
40-
});
41-
42-
return items;
31+
return keys.flatMap((key) => {
32+
return [
33+
{
34+
value: {
35+
[valueKey as Keys<T>]: key,
36+
[displayKey as Keys<T>]: key,
37+
[groupKey as Keys<T>]: key,
38+
} as T,
39+
header: true,
40+
dataIndex: -1,
41+
},
42+
...(grouped.get(key) ?? []),
43+
];
4344
});
4445
}
4546
}

src/components/common/util.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,6 @@ export function findElementFromEventPath<T extends Element>(
168168
return getElementsFromEventPath(event).find(func) as T | undefined;
169169
}
170170

171-
export function groupBy<T>(array: T[], key: keyof T | ((item: T) => any)) {
172-
const result: Record<string, T[]> = {};
173-
const _get = isFunction(key) ? key : (item: T) => item[key];
174-
175-
for (const item of array) {
176-
const category = _get(item);
177-
const group = result[category];
178-
179-
if (Array.isArray(group)) {
180-
group.push(item);
181-
} else {
182-
result[category] = [item];
183-
}
184-
}
185-
186-
return result;
187-
}
188-
189171
export function first<T>(arr: T[]) {
190172
return arr.at(0) as T;
191173
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"target": "es2022",
44
"module": "NodeNext",
5-
"lib": ["es2023", "DOM", "DOM.Iterable"],
5+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
66
"outDir": "dist",
77
"rootDir": "./",
88
"declaration": true,

0 commit comments

Comments
 (0)