Function aggregateGroups

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

    Type Parameters

    • T

      Type of the values in the input record.

    • A

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

    Parameters

    • record: Readonly<Record<string, readonly T[]>>

      The grouping to aggregate.

    • aggregator: ((current: T, key: string, first: boolean, accumulator?: A) => A)

      The function to apply to each group.

        • (current, key, first, accumulator?): A
        • Parameters

          • current: T
          • key: string
          • first: boolean
          • Optionalaccumulator: A

          Returns A

    Returns Record<string, A>

    A record with the same keys as the input record, but with the values being the result of applying the aggregator to each group.

    import { aggregateGroups } from "@std/collections/aggregate-groups";
    import { assertEquals } from "@std/assert";

    const foodProperties = {
    Curry: ["spicy", "vegan"],
    Omelette: ["creamy", "vegetarian"],
    };

    const descriptions = aggregateGroups(
    foodProperties,
    (current, key, first, acc) => {
    return first
    ? `${key} is ${current}`
    : `${acc} and ${current}`;
    },
    );

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