uft
    Preparing search index...

    Function partition

    • Creates two arrays, the first containing the elements for which the predicate returned true, and the second containing the elements for which the predicate returned false.

      Type Parameters

      • T extends readonly unknown[]
      • S extends unknown

      Parameters

      • array: T

        The array to partition.

      • predicate: (element: T[number], index: number) => element is S

        The predicate to match the elements against.

      Returns [
          trueElements: S[],
          falseElements: inferReversePredicate<
              (element: T[number], index: number) => element is S,
          >[],
      ]

      A tuple containing the two arrays.

      const [even, odd] = partition(
      [1, 2, 3, 4, 5],
      (n) => n % 2 === 0 // (true if n is even)
      )
      console.log(even) // [2, 4]
      console.log(odd) // [1, 3, 5]
      import { isString } from 'uft'

      const [strings, other] = partition(
      [1, 'foo', { bar: '' }],
      isString
      )
      strings // type: string[]
      other // type: (number | { bar: string })[]
    • Creates two arrays, the first containing the elements for which the predicate returned true, and the second containing the elements for which the predicate returned false.

      Type Parameters

      • T extends readonly unknown[]
      • S

      Parameters

      • array: T

        The array to partition.

      • predicate: (element: T[number], index: number) => element is S

        The predicate to match the elements against.

      Returns [
          trueElements: AssignableTo<T[number], S>[],
          falseElements: AssignableTo<
              T[number],
              inferReversePredicate<(element: T[number], index: number) => element is S>,
          >[],
      ]

      A tuple containing the two arrays.

      const [even, odd] = partition(
      [1, 2, 3, 4, 5],
      (n) => n % 2 === 0 // (true if n is even)
      )
      console.log(even) // [2, 4]
      console.log(odd) // [1, 3, 5]
      import { isString } from 'uft'

      const [strings, other] = partition(
      [1, 'foo', { bar: '' }],
      isString
      )
      strings // type: string[]
      other // type: (number | { bar: string })[]
    • Creates two arrays, the first containing the elements for which the predicate returned true, and the second containing the elements for which the predicate returned false.

      Type Parameters

      • T extends readonly unknown[]

      Parameters

      • array: T

        The array to partition.

      • predicate: (element: T[number], index: number) => boolean

        The predicate to match the elements against.

      Returns [trueElements: ToMutableArray<T>, falseElements: ToMutableArray<T>]

      A tuple containing the two arrays.

      const [even, odd] = partition(
      [1, 2, 3, 4, 5],
      (n) => n % 2 === 0 // (true if n is even)
      )
      console.log(even) // [2, 4]
      console.log(odd) // [1, 3, 5]
      import { isString } from 'uft'

      const [strings, other] = partition(
      [1, 'foo', { bar: '' }],
      isString
      )
      strings // type: string[]
      other // type: (number | { bar: string })[]