Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fathym_common/src/src.deps.ts>aggregateGroups

The Fathym Reference Architecture provides the common foundation for applications built in Typescript.
Go to Latest
function aggregateGroups
import { aggregateGroups } from "https://deno.land/x/fathym_common@v0.0.158/src/src.deps.ts";

Applies the given aggregator to each group in the given grouping, returning the results together with the respective group keys

Examples

Example 1

import { aggregateGroups } from "https://deno.land/std@0.224.0/collections/aggregate_groups.ts";
import { assertEquals } from "https://deno.land/std@0.224.0/assert/assert_equals.ts";

const foodProperties = {
  "Curry": ["spicy", "vegan"],
  "Omelette": ["creamy", "vegetarian"],
};
const descriptions = aggregateGroups(
  foodProperties,
  (current, key, first, acc) => {
    if (first) {
      return `${key} is ${current}`;
    }

    return `${acc} and ${current}`;
  },
);

assertEquals(descriptions, {
  "Curry": "Curry is spicy and vegan",
  "Omelette": "Omelette is creamy and vegetarian",
});

Type Parameters

T

input type of an item in a group in the given grouping.

A

type of the accumulator value, which will match the returned record's values.

Parameters

record: Readonly<Record<string, ReadonlyArray<T>>>
aggregator: (
current: T,
key: string,
first: boolean,
accumulator?: A,
) => A

Returns

Record<string, A>