function arrayGroupToMap(elements, callback) {
  /** @type {Map<U, Array<T>>} */
  const elementsByValue = new Map();

  for (const element of elements) {
    const key = callback(element);
    const group = elementsByValue.get(key) || [];
    group.push(element);
    elementsByValue.set(key, group);
  }

  return elementsByValue;
}
TODO: replace by Array.prototype.groupToMap when available.
@template {unknown} T
@template {string} U
@param {Array<T>} elements
@param {(element: T) => U} callback
@return {Map<U, Array<T>>}