mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 19:04:46 +00:00
feat(packages/stdlib): add bigint math utils
This commit is contained in:
39
packages/stdlib/src/math/bigint/maxBigInt/index.test.ts
Normal file
39
packages/stdlib/src/math/bigint/maxBigInt/index.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { maxBigInt } from './index';
|
||||
|
||||
describe('maxBigInt', () => {
|
||||
it('returns -Infinity when no values are provided', () => {
|
||||
expect(() => maxBigInt()).toThrow(new TypeError('maxBigInt requires at least one argument'));
|
||||
});
|
||||
|
||||
it('returns the largest value from a list of positive bigints', () => {
|
||||
const result = maxBigInt(10n, 20n, 5n, 15n);
|
||||
expect(result).toBe(20n);
|
||||
});
|
||||
|
||||
it('returns the largest value from a list of negative bigints', () => {
|
||||
const result = maxBigInt(-10n, -20n, -5n, -15n);
|
||||
expect(result).toBe(-5n);
|
||||
});
|
||||
|
||||
it('returns the largest value from a list of mixed positive and negative bigints', () => {
|
||||
const result = maxBigInt(10n, -20n, 5n, -15n);
|
||||
expect(result).toBe(10n);
|
||||
});
|
||||
|
||||
it('returns the value itself when only one bigint is provided', () => {
|
||||
const result = maxBigInt(10n);
|
||||
expect(result).toBe(10n);
|
||||
});
|
||||
|
||||
it('returns the largest value when all values are the same', () => {
|
||||
const result = maxBigInt(10n, 10n, 10n);
|
||||
expect(result).toBe(10n);
|
||||
});
|
||||
|
||||
it('handles a large number of bigints', () => {
|
||||
const values = Array.from({ length: 1000 }, (_, i) => BigInt(i));
|
||||
const result = maxBigInt(...values);
|
||||
expect(result).toBe(999n);
|
||||
});
|
||||
});
|
||||
15
packages/stdlib/src/math/bigint/maxBigInt/index.ts
Normal file
15
packages/stdlib/src/math/bigint/maxBigInt/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Like `Math.max` but for BigInts
|
||||
*
|
||||
* @param {...bigint} values The values to compare
|
||||
* @returns {bigint} The largest value
|
||||
* @throws {TypeError} If no arguments are provided
|
||||
*
|
||||
* @since 0.0.2
|
||||
*/
|
||||
export function maxBigInt(...values: bigint[]) {
|
||||
if (!values.length)
|
||||
throw new TypeError('maxBigInt requires at least one argument');
|
||||
|
||||
return values.reduce((acc, val) => val > acc ? val : acc);
|
||||
}
|
||||
Reference in New Issue
Block a user