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

x/fathym_common/deps.ts>findSingle

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

Returns an element if and only if that element is the only one matching the given condition. Returns undefined otherwise.

Examples

Example 1

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

const bookings = [
  { month: "January", active: false },
  { month: "March", active: false },
  { month: "June", active: true },
];
const activeBooking = findSingle(bookings, (it) => it.active);
const inactiveBooking = findSingle(bookings, (it) => !it.active);

assertEquals(activeBooking, { month: "June", active: true });
assertEquals(inactiveBooking, undefined); // there are two applicable items

Parameters

array: Iterable<T>
predicate: (el: T) => boolean

Returns

T | undefined