es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
20 lines (19 loc) • 799 B
JavaScript
//#region src/_internal/bigIntRangeLength.ts
/**
* Calculates how many elements a bigint range from `start` (inclusive) to `end` (exclusive) contains.
*
* This mirrors `Math.ceil((end - start) / step)` from the `number` implementations, but performs the
* ceiling division with bigint arithmetic. Returns `0` when `step` points away from `end`.
*
* @param start - The start of the range.
* @param end - The end of the range.
* @param step - A non-zero step value.
* @returns The number of elements in the range.
*/
function bigIntRangeLength(start, end, step) {
const difference = end - start;
if (step > 0n) return difference <= 0n ? 0 : Number((difference + step - 1n) / step);
return difference >= 0n ? 0 : Number((difference + step + 1n) / step);
}
//#endregion
export { bigIntRangeLength };