// @ts-check /** * @template T * @param {Array} array * @param {(T) => string} getKey * @returns {Record>} */ const groupBy = (array, getKey) => { /** @type {Record>} */ const table = {}; for (const element of array) { const key = getKey(element); let group = table[key]; if (!group) { group = []; table[key] = group; } group.push(element); } return table; }; /** * @template T * @param {Record>} table * @returns {Record>} */ const sortKeys = table => { /** @type {Record>} */ const sortedTable = {}; for (const key of Object.keys(table).sort()) { sortedTable[key] = table[key]; } return sortedTable; }; module.exports = { groupBy, sortKeys, };