Skip to content
Open
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
57 changes: 29 additions & 28 deletions src/components/combo/operations/group.ts
Original file line number Diff line number Diff line change
@@ -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<T extends object> {
protected orderBy = new Map(
Object.entries({
asc: 1,
desc: -1,
})
);
const OrderBy = Object.freeze({ asc: 1, desc: -1 });

public apply(data: ComboRecord<T>[], controller: DataController<T>) {
export default class GroupDataOperation<T extends object> {
public apply(
data: ComboRecord<T>[],
controller: DataController<T>
): ComboRecord<T>[] {
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<T>]: group,
[displayKey as Keys<T>]: group,
[groupKey as Keys<T>]: group,
} as T,
});

return items;
return keys.flatMap((key) => {
return [
{
value: {
[valueKey as Keys<T>]: key,
[displayKey as Keys<T>]: key,
[groupKey as Keys<T>]: key,
} as T,
header: true,
dataIndex: -1,
},
...grouped.get(key)!,
];
});
}
}
18 changes: 0 additions & 18 deletions src/components/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,6 @@ export function findElementFromEventPath<T extends Element>(
return getElementsFromEventPath(event).find(func) as T | undefined;
}

export function groupBy<T>(array: T[], key: keyof T | ((item: T) => any)) {
const result: Record<string, T[]> = {};
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<T>(arr: T[]) {
return arr.at(0) as T;
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "es2021",
"module": "es2020",
"lib": ["es2023", "DOM", "DOM.Iterable"],
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"outDir": "dist",
"rootDir": "./",
"declaration": true,
Expand Down