// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/**
 * Returns an element if and only if that element is the only one matching the
 * given condition. Returns `undefined` otherwise.
 *
 * @template T The type of the elements in the input array.
 *
 * @param array The array to find a single element in.
 * @param predicate The function to test each element for a condition.
 *
 * @returns The single element that matches the given condition or `undefined`
 * if there are zero or more than one matching elements.
 *
 * @example Basic usage
 * ```ts
 * import { findSingle } from "@std/collections/find-single";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const bookings = [
 *   { month: "January", active: false },
 *   { month: "March", active: false },
 *   { month: "June", active: true },
 * ];
 * const activeBooking = findSingle(bookings, (booking) => booking.active);
 * const inactiveBooking = findSingle(bookings, (booking) => !booking.active);
 *
 * assertEquals(activeBooking, { month: "June", active: true });
 * assertEquals(inactiveBooking, undefined); // There are two applicable items
 * ```
 */
export function findSingle<T>(
  array: Iterable<T>,
  predicate: (el: T) => boolean,
): T | undefined {
  let match: T | undefined;
  let found = false;
  for (const element of array) {
    if (predicate(element)) {
      if (found) return undefined;
      found = true;
      match = element;
    }
  }

  return match;
}

// denoCacheMetadata={"headers":{"content-length":"1526","access-control-expose-headers":"*","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=iiHcXCU61CUVTZpH0Jx0ZcqyhTW79mtPTCmUI615QRNTMJnpEhNp9Doii52bwFK84HWeI%2FL%2FvpicDs1sFEPTnlIB6n32I5iO4OAtoipA20A6ScaKYRxeLnA%3D\"}]}","alt-svc":"h3=\":443\"; ma=86400","cf-ray":"a223e6564efb7db6-LAX","cross-origin-resource-policy":"cross-origin","set-cookie":"__cf_bm=YIKb.G02OsqxOs1tN3xJyk7C8ONswEujQwi12mHi_4k-1785241874.953929-1.0.1.1-V7LT4NS6r..1VGGDEHIj1zH644oMnULf7LVxb.XSVQ8b6Fl2DSpByDAXedrpZ6SS.RQ85uO0EXtbY.q4xHXFMJ3wrTNySBOBmTQbL9RCi6jjukrrGgvo7V4_QvKrjOiN; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","date":"Tue, 28 Jul 2026 12:31:15 GMT","server":"cloudflare","vary":"Origin","x-robots-tag":"noindex","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","content-type":"text/typescript","access-control-allow-origin":"*","cache-control":"public, max-age=31536000, immutable","etag":"\"96cec8481902b34d1491690fc374ac5c\"","x-content-type-options":"nosniff","x-jsr-backend":"modules","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"},"url":"https://jsr.io/@std/collections/0.224.2/find_single.ts","time":1785241875}