1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 10:54:44 +00:00

feat(packages/stdlib): add bigint math utils

This commit is contained in:
2024-09-30 06:20:09 +07:00
parent 061d45f6fd
commit 975ca98f9a
11 changed files with 333 additions and 2 deletions

View File

@@ -0,0 +1,35 @@
import {describe, it, expect} from 'vitest';
import {clampBigInt} from './index';
describe('clampBigInt', () => {
it('clamp a value within the given range', () => {
// value < min
expect(clampBigInt(-10n, 0n, 100n)).toBe(0n);
// value > max
expect(clampBigInt(200n, 0n, 100n)).toBe(100n);
// value within range
expect(clampBigInt(50n, 0n, 100n)).toBe(50n);
// value at min
expect(clampBigInt(0n, 0n, 100n)).toBe(0n);
// value at max
expect(clampBigInt(100n, 0n, 100n)).toBe(100n);
// value at midpoint
expect(clampBigInt(50n, 100n, 100n)).toBe(100n);
});
it('handle edge cases', () => {
// all values are the same
expect(clampBigInt(5n, 5n, 5n)).toBe(5n);
// min > max
expect(clampBigInt(10n, 100n, 50n)).toBe(50n);
// negative range and value
expect(clampBigInt(-10n, -100n, -5n)).toBe(-10n);
});
});