@mirei/ts-collections
    Preparing search index...

    Function defaultIfEmpty

    • Supplies fallback content when the sequence contains no elements.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • Optionalvalue: TElement | null

        Optional value returned in a singleton sequence when the source is empty. Defaults to null.

      Returns IEnumerable<TElement | null>

      The original sequence when it has elements; otherwise, a singleton sequence containing the provided value.

      Use this to ensure downstream operators always receive at least one element.

      const empty = [];
      const withDefault = defaultIfEmpty(empty, 0).toArray();
      console.log(withDefault); // [0]

      const numbers = [1, 2, 3];
      const withDefault2 = defaultIfEmpty(numbers, 0).toArray();
      console.log(withDefault2); // [1, 2, 3]