array-view-ts
23 строки · 960.0 Байт
1import { 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*/
13export function normalizeIndex(index: number, containerLength: number, throwError: boolean = true): number {14const dist = index >= 0 ? index : Math.abs(index) - 1;15if (throwError && dist >= containerLength) {16throw new IndexError(`Index ${index} is out of range.`);17}18return index < 0 ? containerLength + index : index;19}
20
21export function isCountable(target: any): boolean {22return target.length !== undefined;23}
24