JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr{ gilour

File "group-array-by.ts"

Full Path: /home/markqprx/iniasli.pro/resources/client/utils/array/group-array-by.ts
File size: 576 bytes
MIME-type: text/plain
Charset: utf-8

interface Options<T> {
  map?: (item: T) => T;
}

export function groupArrayBy<T>(
  arr: T[],
  cb: (item: any) => string,
  options?: Options<T>,
): {[key: string]: T[]} {
  const result: {[key: string]: T[]} = {};
  for (let i = 0; i < arr.length; i++) {
    let item = arr[i];
    const bucketCategory = cb(item);
    const bucket = result[bucketCategory];

    item = options?.map ? options.map(arr[i]) : arr[i];

    if (!Array.isArray(bucket)) {
      result[bucketCategory] = [item];
    } else {
      result[bucketCategory].push(item);
    }
  }

  return result;
}