| 144 | * @returns {ListOfCommonItems} list of common items |
| 145 | */ |
| 146 | const popCommonItems = (itemsSet, getKey, condition) => { |
| 147 | /** @type {Map<string, string[]>} */ |
| 148 | const map = new Map(); |
| 149 | for (const item of itemsSet) { |
| 150 | const key = getKey(item); |
| 151 | if (key) { |
| 152 | let list = map.get(key); |
| 153 | if (list === undefined) { |
| 154 | /** @type {string[]} */ |
| 155 | list = []; |
| 156 | map.set(key, list); |
| 157 | } |
| 158 | list.push(item); |
| 159 | } |
| 160 | } |
| 161 | /** @type {ListOfCommonItems} */ |
| 162 | const result = []; |
| 163 | for (const list of map.values()) { |
| 164 | if (condition(list)) { |
| 165 | for (const item of list) { |
| 166 | itemsSet.delete(item); |
| 167 | } |
| 168 | result.push(list); |
| 169 | } |
| 170 | } |
| 171 | return result; |
| 172 | }; |
| 173 | |
| 174 | /** |
| 175 | * Gets common prefix. |