array-view-ts

Форк
0
/
utils.ts 
23 строки · 960.0 Байт
1
import { IndexError } from "./excpetions";
2

3
/**
4
 * Normalizes an index value within the range of a container length,
5
 * optionally throwing an error if the index is out of range.
6
 *
7
 * @param {number} index - The index value to normalize.
8
 * @param {number} containerLength - The length of the container or array.
9
 * @param {boolean} [throwError=true] - Optional flag to indicate whether to throw an error if the index is out of range.
10
 *
11
 * @returns {number} The normalized index value within the range of the container length.
12
 */
13
export function normalizeIndex(index: number, containerLength: number, throwError: boolean = true): number {
14
  const dist = index >= 0 ? index : Math.abs(index) - 1;
15
  if (throwError && dist >= containerLength) {
16
    throw new IndexError(`Index ${index} is out of range.`);
17
  }
18
  return index < 0 ? containerLength + index : index;
19
}
20

21
export function isCountable(target: any): boolean {
22
  return target.length !== undefined;
23
}
24

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.