(items, groupConfigs)
| 61 | * @returns {(I | G)[]} grouped items |
| 62 | */ |
| 63 | const smartGrouping = (items, groupConfigs) => { |
| 64 | /** @type {Items<I, G>} */ |
| 65 | const itemsWithGroups = new Set(); |
| 66 | /** @type {Map<string, Group<I, G>>} */ |
| 67 | const allGroups = new Map(); |
| 68 | for (const item of items) { |
| 69 | /** @type {Groups<I, G>} */ |
| 70 | const groups = new Set(); |
| 71 | for (let i = 0; i < groupConfigs.length; i++) { |
| 72 | const groupConfig = groupConfigs[i]; |
| 73 | const keys = groupConfig.getKeys(item); |
| 74 | if (keys) { |
| 75 | for (const name of keys) { |
| 76 | const key = `${i}:${name}`; |
| 77 | let group = allGroups.get(key); |
| 78 | if (group === undefined) { |
| 79 | allGroups.set( |
| 80 | key, |
| 81 | (group = { |
| 82 | config: groupConfig, |
| 83 | name, |
| 84 | alreadyGrouped: false, |
| 85 | items: undefined |
| 86 | }) |
| 87 | ); |
| 88 | } |
| 89 | groups.add(group); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | itemsWithGroups.add({ |
| 94 | item, |
| 95 | groups |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns groups items. |
| 101 | * @param {Items<I, G>} itemsWithGroups input items with groups |
| 102 | * @returns {(I | G)[]} groups items |
| 103 | */ |
| 104 | const runGrouping = (itemsWithGroups) => { |
| 105 | const totalSize = itemsWithGroups.size; |
| 106 | for (const entry of itemsWithGroups) { |
| 107 | for (const group of entry.groups) { |
| 108 | if (group.alreadyGrouped) continue; |
| 109 | const items = group.items; |
| 110 | if (items === undefined) { |
| 111 | group.items = new Set([entry]); |
| 112 | } else { |
| 113 | items.add(entry); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | /** @type {Map<Group<I, G>, { items: Items<I, G>, options: GroupOptions | false | undefined, used: boolean }>} */ |
| 118 | const groupMap = new Map(); |
| 119 | for (const group of allGroups.values()) { |
| 120 | if (group.items) { |
no test coverage detected