UNPKG

random-pie

Version:

A lightweight TypeScript/JavaScript library providing Python-style random number generation and randomization utilities. This utility module implements the most common functions from Python's random module, making it intuitive for Python developers workin

24 lines (23 loc) 739 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.accumulate = accumulate; /** * Calculates the cumulative sum of an array * @param arr The input array of numbers * @returns An array of cumulative sums * @throws TypeError if the argument is not an array * @throws RangeError if the input array is empty */ function accumulate(arr) { if (!Array.isArray(arr)) { throw new TypeError("Argument must be an array"); } if (arr.length === 0) { throw new RangeError("Input array cannot be empty"); } return arr.reduce((acc, curr) => { const nextSum = (acc.length > 0 ? acc[acc.length - 1] : 0) + curr; acc.push(nextSum); return acc; }, []); }