Function maxOf

  • Applies the given selector to all elements of the provided collection and returns the max value of all elements. If an empty array is provided the function will return undefined.

    Type Parameters

    • T

      The type of the elements in the array.

    Parameters

    • array: Iterable<T, any, any>

      The array to find the maximum element in.

    • selector: ((el: T) => number)

      The function to get the value to compare from each element.

        • (el): number
        • Parameters

          • el: T

          Returns number

    Returns number | undefined

    The largest value of the given function or undefined if there are no elements.

    import { maxOf } from "@std/collections/max-of";
    import { assertEquals } from "@std/assert";

    const inventory = [
    { name: "mustard", count: 2 },
    { name: "soy", count: 4 },
    { name: "tomato", count: 32 },
    ];

    const maxCount = maxOf(inventory, (item) => item.count);

    assertEquals(maxCount, 32);
  • Applies the given selector to all elements of the provided collection and returns the max value of all elements. If an empty array is provided the function will return undefined.

    Type Parameters

    • T

      The type of the elements in the array.

    Parameters

    • array: Iterable<T, any, any>

      The array to find the maximum element in.

    • selector: ((el: T) => bigint)

      The function to get the value to compare from each element.

        • (el): bigint
        • Parameters

          • el: T

          Returns bigint

    Returns bigint | undefined

    The first element that is the largest value of the given function or undefined if there are no elements.

    import { maxOf } from "@std/collections/max-of";
    import { assertEquals } from "@std/assert";

    const inventory = [
    { name: "mustard", count: 2n },
    { name: "soy", count: 4n },
    { name: "tomato", count: 32n },
    ];

    const maxCount = maxOf(inventory, (i) => i.count);

    assertEquals(maxCount, 32n);