1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
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 | const smartGrouping = (items, groupConfigs) => {
|
46 |
|
47 | const itemsWithGroups = new Set();
|
48 |
|
49 | const allGroups = new Map();
|
50 | for (const item of items) {
|
51 |
|
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);
|
56 | if (keys) {
|
57 | for (const name of keys) {
|
58 | const key = `${i}:${name}`;
|
59 | let group = allGroups.get(key);
|
60 | if (group === undefined) {
|
61 | allGroups.set(
|
62 | key,
|
63 | (group = {
|
64 | config: groupConfig,
|
65 | name,
|
66 | alreadyGrouped: false,
|
67 | items: undefined
|
68 | })
|
69 | );
|
70 | }
|
71 | groups.add(group);
|
72 | }
|
73 | }
|
74 | }
|
75 | itemsWithGroups.add({
|
76 | item,
|
77 | groups
|
78 | });
|
79 | }
|
80 | |
81 |
|
82 |
|
83 |
|
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]);
|
92 | } else {
|
93 | items.add(entry);
|
94 | }
|
95 | }
|
96 | }
|
97 |
|
98 | const groupMap = new Map();
|
99 | for (const group of allGroups.values()) {
|
100 | if (group.items) {
|
101 | const items = group.items;
|
102 | group.items = undefined;
|
103 | groupMap.set(group, {
|
104 | items,
|
105 | options: undefined,
|
106 | used: false
|
107 | });
|
108 | }
|
109 | }
|
110 |
|
111 | const results = [];
|
112 | for (;;) {
|
113 |
|
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(
|
126 | group.name,
|
127 | Array.from(items, ({ item }) => item)
|
128 | )) ||
|
129 | false;
|
130 | }
|
131 |
|
132 | const force = options && options.force;
|
133 | if (!force) {
|
134 | if (bestGroupOptions && bestGroupOptions.force) continue;
|
135 | if (used) continue;
|
136 | if (items.size <= 1 || totalSize - items.size <= 1) {
|
137 | continue;
|
138 | }
|
139 | }
|
140 | const targetGroupCount = (options && options.targetGroupCount) || 4;
|
141 | let sizeValue = force
|
142 | ? items.size
|
143 | : Math.min(
|
144 | items.size,
|
145 | (totalSize * 2) / targetGroupCount +
|
146 | itemsWithGroups.size -
|
147 | items.size
|
148 | );
|
149 | if (
|
150 | sizeValue > bestGroupSize ||
|
151 | (force && (!bestGroupOptions || !bestGroupOptions.force))
|
152 | ) {
|
153 | bestGroup = group;
|
154 | bestGroupSize = sizeValue;
|
155 | bestGroupItems = items;
|
156 | bestGroupOptions = options;
|
157 | }
|
158 | }
|
159 | if (bestGroup === undefined) {
|
160 | break;
|
161 | }
|
162 | const items = new Set(bestGroupItems);
|
163 | const options = bestGroupOptions;
|
164 |
|
165 | const groupChildren = !options || options.groupChildren !== false;
|
166 |
|
167 | for (const item of items) {
|
168 | itemsWithGroups.delete(item);
|
169 |
|
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);
|
176 | } else {
|
177 | state.options = undefined;
|
178 | if (groupChildren) {
|
179 | state.used = true;
|
180 | }
|
181 | }
|
182 | }
|
183 | }
|
184 | }
|
185 | groupMap.delete(bestGroup);
|
186 |
|
187 | const key = bestGroup.name;
|
188 | const groupConfig = bestGroup.config;
|
189 |
|
190 | const allItems = Array.from(items, ({ item }) => item);
|
191 |
|
192 | bestGroup.alreadyGrouped = true;
|
193 | const children = groupChildren ? runGrouping(items) : allItems;
|
194 | bestGroup.alreadyGrouped = false;
|
195 |
|
196 | results.push(groupConfig.createGroup(key, children, allItems));
|
197 | }
|
198 | for (const { item } of itemsWithGroups) {
|
199 | results.push(item);
|
200 | }
|
201 | return results;
|
202 | };
|
203 | return runGrouping(itemsWithGroups);
|
204 | };
|
205 |
|
206 | module.exports = smartGrouping;
|