Back to Functions

takeRightWhile

arrays

Creates a slice of array with elements taken from the end. Elements are taken until predicate returns falsey.

Installation

Import
import { takeRightWhile } from '@tulx/utils';

Source Code

Implementation
/**
 * Creates a slice of array with elements taken from the end.
 * Elements are taken until predicate returns falsey.
 *
 * @param array - The array to query.
 * @param predicate - The function invoked per iteration.
 * @returns The slice of array.
 *
 * @example
 * ```ts
 * const users = [
 *   { 'user': 'barney', 'active': true },
 *   { 'user': 'fred', 'active': false },
 *   { 'user': 'pebbles', 'active': false }
 * ];
 * takeRightWhile(users, (o) => !o.active);
 * // [{ 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': false }]
 * ```
 */
export function takeRightWhile<T>(
  array: readonly T[],
  predicate: (value: T, index: number, array: readonly T[]) => boolean
): T[] {
  let startIndex = 0;
  for (let i = array.length - 1; i >= 0; i--) {
    if (!predicate(array[i], i, array)) {
      startIndex = i + 1;
      break;
    }
  }
  return array.slice(startIndex);
}

Example

import { takeRightWhile } from '@tulx/utils';

const users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
takeRightWhile(users, (o) => !o.active);
// [{ 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': false }]

Related Functions