smartGrouping.js
5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
/**
* @typedef {Object} GroupOptions
* @property {boolean=} groupChildren
* @property {boolean=} force
* @property {number=} targetGroupCount
*/
/**
* @template T
* @template R
* @typedef {Object} GroupConfig
* @property {function(T): string[]} getKeys
* @property {function(string, (R | T)[], T[]): R} createGroup
* @property {function(string, T[]): GroupOptions=} getOptions
*/
/**
* @template T
* @template R
* @typedef {Object} ItemWithGroups
* @property {T} item
* @property {Set<Group<T, R>>} groups
*/
/**
* @template T
* @template R
* @typedef {{ config: GroupConfig<T, R>, name: string, alreadyGrouped: boolean, items: Set<ItemWithGroups<T, R>> | undefined }} Group
*/
/**
* @template T
* @template R
* @param {T[]} items the list of items
* @param {GroupConfig<T, R>[]} groupConfigs configuration
* @returns {(R | T)[]} grouped items
*/
const smartGrouping = (items, groupConfigs) => {
/** @type {Set<ItemWithGroups<T, R>>} */
const itemsWithGroups = new Set();
/** @type {Map<string, Group<T, R>>} */
const allGroups = new Map();
for (const item of items) {
/** @type {Set<Group<T, R>>} */
const groups = new Set();
for (let i = 0; i < groupConfigs.length; i++) {
const groupConfig = groupConfigs[i];
const keys = groupConfig.getKeys(item);
if (keys) {
for (const name of keys) {
const key = `${i}:${name}`;
let group = allGroups.get(key);
if (group === undefined) {
allGroups.set(
key,
(group = {
config: groupConfig,
name,
alreadyGrouped: false,
items: undefined
})
);
}
groups.add(group);
}
}
}
itemsWithGroups.add({
item,
groups
});
}
/**
* @param {Set<ItemWithGroups<T, R>>} itemsWithGroups input items with groups
* @returns {(T | R)[]} groups items
*/
const runGrouping = itemsWithGroups => {
const totalSize = itemsWithGroups.size;
for (const entry of itemsWithGroups) {
for (const group of entry.groups) {
if (group.alreadyGrouped) continue;
const items = group.items;
if (items === undefined) {
group.items = new Set([entry]);
} else {
items.add(entry);
}
}
}
/** @type {Map<Group<T, R>, { items: Set<ItemWithGroups<T, R>>, options: GroupOptions | false | undefined, used: boolean }>} */
const groupMap = new Map();
for (const group of allGroups.values()) {
if (group.items) {
const items = group.items;
group.items = undefined;
groupMap.set(group, {
items,
options: undefined,
used: false
});
}
}
/** @type {(T | R)[]} */
const results = [];
for (;;) {
/** @type {Group<T, R>} */
let bestGroup = undefined;
let bestGroupSize = -1;
let bestGroupItems = undefined;
let bestGroupOptions = undefined;
for (const [group, state] of groupMap) {
const { items, used } = state;
let options = state.options;
if (options === undefined) {
const groupConfig = group.config;
state.options = options =
(groupConfig.getOptions &&
groupConfig.getOptions(
group.name,
Array.from(items, ({ item }) => item)
)) ||
false;
}
const force = options && options.force;
if (!force) {
if (bestGroupOptions && bestGroupOptions.force) continue;
if (used) continue;
if (items.size <= 1 || totalSize - items.size <= 1) {
continue;
}
}
const targetGroupCount = (options && options.targetGroupCount) || 4;
let sizeValue = force
? items.size
: Math.min(
items.size,
(totalSize * 2) / targetGroupCount +
itemsWithGroups.size -
items.size
);
if (
sizeValue > bestGroupSize ||
(force && (!bestGroupOptions || !bestGroupOptions.force))
) {
bestGroup = group;
bestGroupSize = sizeValue;
bestGroupItems = items;
bestGroupOptions = options;
}
}
if (bestGroup === undefined) {
break;
}
const items = new Set(bestGroupItems);
const options = bestGroupOptions;
const groupChildren = !options || options.groupChildren !== false;
for (const item of items) {
itemsWithGroups.delete(item);
// Remove all groups that items have from the map to not select them again
for (const group of item.groups) {
const state = groupMap.get(group);
if (state !== undefined) {
state.items.delete(item);
if (state.items.size === 0) {
groupMap.delete(group);
} else {
state.options = undefined;
if (groupChildren) {
state.used = true;
}
}
}
}
}
groupMap.delete(bestGroup);
const key = bestGroup.name;
const groupConfig = bestGroup.config;
const allItems = Array.from(items, ({ item }) => item);
bestGroup.alreadyGrouped = true;
const children = groupChildren ? runGrouping(items) : allItems;
bestGroup.alreadyGrouped = false;
results.push(groupConfig.createGroup(key, children, allItems));
}
for (const { item } of itemsWithGroups) {
results.push(item);
}
return results;
};
return runGrouping(itemsWithGroups);
};
module.exports = smartGrouping;