diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts index f5f5c27fab6..0e8e38dfdcf 100644 --- a/src/modules/number/index.ts +++ b/src/modules/number/index.ts @@ -373,7 +373,7 @@ export class NumberModule extends SimpleModuleBase { * The bounds are inclusive. * * @param options Maximum value or options object. - * @param options.min Lower bound for generated bigint. Defaults to `0n`. + * @param options.min Lower bound for generated bigint. Defaults to `0n` or `max - 999999999999999n` if `max` is negative. * @param options.max Upper bound for generated bigint. Defaults to `min + 999999999999999n`. * * @throws When `min` is greater than `max`. @@ -397,7 +397,7 @@ export class NumberModule extends SimpleModuleBase { /** * Lower bound for generated bigint. * - * @default 0n + * @default 0n or max - 999999999999999n if max is negative. */ min?: bigint | number | string | boolean; /** @@ -419,7 +419,12 @@ export class NumberModule extends SimpleModuleBase { }; } - const min = BigInt(options.min ?? 0); + const min = + options.min === undefined + ? 0n <= BigInt(options.max ?? 0n + BigInt(999999999999999)) + ? 0n + : BigInt(options.max ?? -999999999999999) + : BigInt(options.min); const max = BigInt(options.max ?? min + BigInt(999999999999999)); if (max === min) { diff --git a/test/modules/number.spec.ts b/test/modules/number.spec.ts index 9af0d58a6f9..7671c3604e2 100644 --- a/test/modules/number.spec.ts +++ b/test/modules/number.spec.ts @@ -590,6 +590,10 @@ describe('number', () => { expect(faker.number.bigInt({ max: 0n })).toBe(0n); }); + it('should return a random bigint given a maximum value of -1', () => { + expect(faker.number.bigInt({ max: -1n })).toBeLessThanOrEqual(-1n); + }); + it('should return a random bigint given a negative bigint minimum and maximum value of 0', () => { const generateBigInt = faker.number.bigInt({ min: -100n, max: 0n }); expect(generateBigInt).toBeGreaterThanOrEqual(-100n);