Back to Functions

rangeRight

util

This method is like range except that it populates values in descending order.

Installation

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

Source Code

Implementation
/**
 * This method is like range except that it populates values in descending order.
 *
 * @param start - The start of the range.
 * @param end - The end of the range.
 * @param step - The value to increment or decrement by.
 * @returns Returns the range of numbers.
 *
 * @example
 * ```ts
 * rangeRight(4); // [3, 2, 1, 0]
 * rangeRight(-4); // [-3, -2, -1, 0]
 * rangeRight(1, 5); // [4, 3, 2, 1]
 * rangeRight(0, 20, 5); // [15, 10, 5, 0]
 * rangeRight(0, -4, -1); // [-3, -2, -1, 0]
 * rangeRight(1, 4, 0); // [1, 1, 1]
 * rangeRight(0); // []
 * ```
 */
export function rangeRight(
  start: number,
  end?: number,
  step?: number
): number[] {
  if (end === undefined) {
    end = start;
    start = 0;
  }

  if (step === undefined) {
    step = start < end ? 1 : -1;
  }

  if (step === 0) {
    return Array(Math.abs(end - start)).fill(start);
  }

  const result: number[] = [];
  let current = start;

  if (step > 0) {
    while (current < end) {
      result.push(current);
      current += step;
    }
  } else {
    while (current > end) {
      result.push(current);
      current += step;
    }
  }

  return result.reverse();
}

Example

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

rangeRight(4); // [3, 2, 1, 0]
rangeRight(-4); // [-3, -2, -1, 0]
rangeRight(1, 5); // [4, 3, 2, 1]
rangeRight(0, 20, 5); // [15, 10, 5, 0]
rangeRight(0, -4, -1); // [-3, -2, -1, 0]
rangeRight(1, 4, 0); // [1, 1, 1]
rangeRight(0); // []

Related Functions