2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
9 * @typedef {Object} GroupOptions
10 * @property {boolean=} groupChildren
11 * @property {boolean=} force
12 * @property {number=} targetGroupCount
18 * @typedef {Object} GroupConfig
19 * @property {function(T): string[]} getKeys
20 * @property {function(string, (R | T)[], T[]): R} createGroup
21 * @property {function(string, T[]): GroupOptions=} getOptions
27 * @typedef {Object} ItemWithGroups
29 * @property {Set<Group<T, R>>} groups
35 * @typedef {{ config: GroupConfig<T, R>, name: string, alreadyGrouped: boolean, items: Set<ItemWithGroups<T, R>> | undefined }} Group
41 * @param {T[]} items the list of items
42 * @param {GroupConfig<T, R>[]} groupConfigs configuration
43 * @returns {(R | T)[]} grouped items
45 const smartGrouping = (items, groupConfigs) => {
46 /** @type {Set<ItemWithGroups<T, R>>} */
47 const itemsWithGroups = new Set();
48 /** @type {Map<string, Group<T, R>>} */
49 const allGroups = new Map();
50 for (const item of items) {
51 /** @type {Set<Group<T, R>>} */
52 const groups = new Set();
53 for (let i = 0; i < groupConfigs.length; i++) {
54 const groupConfig = groupConfigs[i];
55 const keys = groupConfig.getKeys(item);
57 for (const name of keys) {
58 const key = `${i}:${name}`;
59 let group = allGroups.get(key);
60 if (group === undefined) {
66 alreadyGrouped: false,
81 * @param {Set<ItemWithGroups<T, R>>} itemsWithGroups input items with groups
82 * @returns {(T | R)[]} groups items
84 const runGrouping = itemsWithGroups => {
85 const totalSize = itemsWithGroups.size;
86 for (const entry of itemsWithGroups) {
87 for (const group of entry.groups) {
88 if (group.alreadyGrouped) continue;
89 const items = group.items;
90 if (items === undefined) {
91 group.items = new Set([entry]);
97 /** @type {Map<Group<T, R>, { items: Set<ItemWithGroups<T, R>>, options: GroupOptions | false | undefined, used: boolean }>} */
98 const groupMap = new Map();
99 for (const group of allGroups.values()) {
101 const items = group.items;
102 group.items = undefined;
103 groupMap.set(group, {
110 /** @type {(T | R)[]} */
113 /** @type {Group<T, R>} */
114 let bestGroup = undefined;
115 let bestGroupSize = -1;
116 let bestGroupItems = undefined;
117 let bestGroupOptions = undefined;
118 for (const [group, state] of groupMap) {
119 const { items, used } = state;
120 let options = state.options;
121 if (options === undefined) {
122 const groupConfig = group.config;
123 state.options = options =
124 (groupConfig.getOptions &&
125 groupConfig.getOptions(
127 Array.from(items, ({ item }) => item)
132 const force = options && options.force;
134 if (bestGroupOptions && bestGroupOptions.force) continue;
136 if (items.size <= 1 || totalSize - items.size <= 1) {
140 const targetGroupCount = (options && options.targetGroupCount) || 4;
141 let sizeValue = force
145 (totalSize * 2) / targetGroupCount +
146 itemsWithGroups.size -
150 sizeValue > bestGroupSize ||
151 (force && (!bestGroupOptions || !bestGroupOptions.force))
154 bestGroupSize = sizeValue;
155 bestGroupItems = items;
156 bestGroupOptions = options;
159 if (bestGroup === undefined) {
162 const items = new Set(bestGroupItems);
163 const options = bestGroupOptions;
165 const groupChildren = !options || options.groupChildren !== false;
167 for (const item of items) {
168 itemsWithGroups.delete(item);
169 // Remove all groups that items have from the map to not select them again
170 for (const group of item.groups) {
171 const state = groupMap.get(group);
172 if (state !== undefined) {
173 state.items.delete(item);
174 if (state.items.size === 0) {
175 groupMap.delete(group);
177 state.options = undefined;
185 groupMap.delete(bestGroup);
187 const key = bestGroup.name;
188 const groupConfig = bestGroup.config;
190 const allItems = Array.from(items, ({ item }) => item);
192 bestGroup.alreadyGrouped = true;
193 const children = groupChildren ? runGrouping(items) : allItems;
194 bestGroup.alreadyGrouped = false;
196 results.push(groupConfig.createGroup(key, children, allItems));
198 for (const { item } of itemsWithGroups) {
203 return runGrouping(itemsWithGroups);
206 module.exports = smartGrouping;