minWith

import { minWith } from "jsr:@ende/collections";

function

function minWith<T>(array: Iterable<T>, comparator: (a: T, b: T) => number): T | undefined

Returns the first element having the smallest value according to the provided comparator or undefined if there are no elements.

array: Iterable<T>

The array to find the minimum element in.

comparator: (a: T, b: T) => number

The function to compare elements.

Returns

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

Example: Basic usage

import { minWith } from "@ende/collections/min-with";
import { assertEquals } from "@ende/assert";

const people = ["Kim", "Anna", "John"];
const smallestName = minWith(people, (a, b) => a.length - b.length);

assertEquals(smallestName, "Kim");

Source