Hyperlinkv0.8.0-beta.28

Record

Record.findFirstconsteffect/Record.ts:1533
<K extends string | symbol, V, V2 extends V>(
  refinement: (value: NoInfer<V>, key: NoInfer<K>) => value is V2
): (self: ReadonlyRecord<K, V>) => Option.Option<[K, V2]>
<K extends string | symbol, V>(
  predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean
): (self: ReadonlyRecord<K, V>) => Option.Option<[K, V]>
<K extends string | symbol, V, V2 extends V>(
  self: ReadonlyRecord<K, V>,
  refinement: (value: NoInfer<V>, key: NoInfer<K>) => value is V2
): Option.Option<[K, V2]>
<K extends string | symbol, V>(
  self: ReadonlyRecord<K, V>,
  predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean
): Option.Option<[K, V]>

Returns the first entry that satisfies the specified predicate, or None if no such entry exists.

Example (Finding the first matching entry)

import { Record } from "effect"

const record = { a: 1, b: 2, c: 3 }
const result = Record.findFirst(
  record,
  (value, key) => value > 1 && key !== "b"
)
console.log(result) // Option.Some(["c", 3])
elements
Source effect/Record.ts:153331 lines
export const findFirst: {
  <K extends string | symbol, V, V2 extends V>(
    refinement: (value: NoInfer<V>, key: NoInfer<K>) => value is V2
  ): (self: ReadonlyRecord<K, V>) => Option.Option<[K, V2]>
  <K extends string | symbol, V>(
    predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean
  ): (self: ReadonlyRecord<K, V>) => Option.Option<[K, V]>
  <K extends string | symbol, V, V2 extends V>(
    self: ReadonlyRecord<K, V>,
    refinement: (value: NoInfer<V>, key: NoInfer<K>) => value is V2
  ): Option.Option<[K, V2]>
  <K extends string | symbol, V>(
    self: ReadonlyRecord<K, V>,
    predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean
  ): Option.Option<[K, V]>
} = dual(
  2,
  <K extends string | symbol, V>(
    self: ReadonlyRecord<K, V>,
    f: (value: V, key: K) => boolean
  ): Option.Option<[K, V]> => {
    const k = keys(self)
    for (let i = 0; i < k.length; i++) {
      const key = k[i]
      if (f(self[key], key)) {
        return Option.some([key, self[key]])
      }
    }
    return Option.none()
  }
)