Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(number): bigint multipleOf #3402

Open
wants to merge 29 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
29fdabc
feat(number): bigInt multipleOf - has option
soc221b Feb 16, 2025
7fc7c9d
refactor: fix typo
soc221b Feb 16, 2025
a3352e0
feat(number): bigInt multipleOf - must be positive
soc221b Feb 16, 2025
d868592
feat(number): bigInt multipleOf - larger than the given max
soc221b Feb 16, 2025
a2b3dd2
feat(number): bigInt multipleOf - update description
soc221b Feb 16, 2025
6100bf5
feat(number): bigInt multipleOf - implement
soc221b Feb 16, 2025
1cb6b03
test: fix test name
soc221b Feb 16, 2025
c28fda7
style: lint
soc221b Feb 16, 2025
82fa428
feat: add param and example
soc221b Feb 16, 2025
2aa0270
feat: add throws
soc221b Feb 16, 2025
8ed0e2a
Merge branch 'next' into feat/bigint-multiple-of
ST-DDT Feb 16, 2025
6b70cab
Merge branch 'next' into feat/bigint-multiple-of
ST-DDT Feb 17, 2025
0db440d
test: it should throw if there is no suitable bigint value between mi…
soc221b Feb 22, 2025
98b18d5
test: should generate a random bigint with a given max value less tha…
soc221b Feb 22, 2025
bce50d2
test: it should generate a suitable bigint value between negative min…
soc221b Feb 22, 2025
c9cfa2b
fix: bigint
soc221b Feb 22, 2025
1f3add8
feat: ensure error messages follow the same format
soc221b Feb 22, 2025
66f7a45
test: it should throw if there is no suitable bigint value between sa…
soc221b Feb 22, 2025
7af0457
style: format
soc221b Feb 22, 2025
fd245e9
fix: it should generate a suitable bigint value between negative min …
soc221b Feb 22, 2025
a900efb
fix: it should returns inclusive bounds
soc221b Feb 22, 2025
659fa09
refactor: use BigInt
soc221b Feb 22, 2025
d5379c2
Merge branch 'next' into feat/bigint-multiple-of
ST-DDT Feb 23, 2025
53bff61
Merge branch 'next' into feat/bigint-multiple-of
ST-DDT Feb 28, 2025
54afb88
refactor: #discussion_r1976176202
soc221b Mar 1, 2025
042c058
refactor: #discussion_r1976178977, #discussion_r1976179961
soc221b Mar 1, 2025
a8ab42d
fix: #discussion_r1976181511
soc221b Mar 1, 2025
bc6deac
fix: add throws
soc221b Mar 1, 2025
c563103
Merge branch 'next' into feat/bigint-multiple-of
ST-DDT Mar 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/modules/number/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,12 @@ export class NumberModule extends SimpleModuleBase {
* @default min + 999999999999999n
*/
max?: bigint | number | string | boolean;
/**
* The generated bigint will be a multiple of this parameter.
*
* @default 1n
*/
multipleOf?: bigint | number | string | boolean;
} = {}
): bigint {
if (
Expand All @@ -421,13 +427,24 @@ export class NumberModule extends SimpleModuleBase {

const min = BigInt(options.min ?? 0);
const max = BigInt(options.max ?? min + BigInt(999999999999999));
const multipleOf = BigInt(options.multipleOf ?? 1);

if (max === min) {
return min;
}

if (max < min) {
throw new FakerError(`Max ${max} should be larger then min ${min}.`);
throw new FakerError(`Max ${max} should be larger than min ${min}.`);
}

if (multipleOf <= 0n) {
throw new FakerError(`multipleOf should be greater than 0n.`);
}

if (1n < multipleOf && max < multipleOf) {
throw new FakerError(
`Multiple of ${multipleOf}n should be less than or equal to max ${max}n.`
);
}

const delta = max - min;
Expand All @@ -438,10 +455,11 @@ export class NumberModule extends SimpleModuleBase {
length: delta.toString(10).length,
allowLeadingZeros: true,
})
) %
(delta + BigInt(1));
) % delta;

const remaining = multipleOf - ((min + offset) % multipleOf);

return min + offset;
return min + offset + remaining;
}

/**
Expand Down
44 changes: 25 additions & 19 deletions test/modules/__snapshots__/number.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`number > 42 > bigInt > noArgs 1`] = `397511086709821n`;
exports[`number > 42 > bigInt > noArgs 1`] = `397511086709822n`;

exports[`number > 42 > bigInt > with big options 1`] = `19556777749482489605814694n`;
exports[`number > 42 > bigInt > with big options 1`] = `19556777749482489605814696n`;

exports[`number > 42 > bigInt > with bigint value 1`] = `25n`;
exports[`number > 42 > bigInt > with bigint value 1`] = `29n`;

exports[`number > 42 > bigInt > with boolean value 1`] = `1n`;

exports[`number > 42 > bigInt > with number value 1`] = `39n`;
exports[`number > 42 > bigInt > with multipleOf 1`] = `397511086716281n`;

exports[`number > 42 > bigInt > with options 1`] = `19n`;
exports[`number > 42 > bigInt > with number value 1`] = `40n`;

exports[`number > 42 > bigInt > with string value 1`] = `39n`;
exports[`number > 42 > bigInt > with options 1`] = `23n`;

exports[`number > 42 > bigInt > with string value 1`] = `40n`;

exports[`number > 42 > binary > noArgs 1`] = `"0"`;

Expand Down Expand Up @@ -64,19 +66,21 @@ exports[`number > 42 > romanNumeral > with only max 1`] = `"LXII"`;

exports[`number > 42 > romanNumeral > with only min 1`] = `"MDI"`;

exports[`number > 1211 > bigInt > noArgs 1`] = `982966736876848n`;
exports[`number > 1211 > bigInt > noArgs 1`] = `982966736876849n`;

exports[`number > 1211 > bigInt > with big options 1`] = `25442250580110979794946298n`;
exports[`number > 1211 > bigInt > with big options 1`] = `25442250580110979794946302n`;

exports[`number > 1211 > bigInt > with bigint value 1`] = `114n`;
exports[`number > 1211 > bigInt > with bigint value 1`] = `122n`;

exports[`number > 1211 > bigInt > with boolean value 1`] = `1n`;

exports[`number > 1211 > bigInt > with number value 1`] = `12n`;
exports[`number > 1211 > bigInt > with multipleOf 1`] = `982966736876952n`;

exports[`number > 1211 > bigInt > with number value 1`] = `15n`;

exports[`number > 1211 > bigInt > with options 1`] = `44n`;
exports[`number > 1211 > bigInt > with options 1`] = `53n`;

exports[`number > 1211 > bigInt > with string value 1`] = `28n`;
exports[`number > 1211 > bigInt > with string value 1`] = `30n`;

exports[`number > 1211 > binary > noArgs 1`] = `"1"`;

Expand Down Expand Up @@ -128,19 +132,21 @@ exports[`number > 1211 > romanNumeral > with only max 1`] = `"CLIV"`;

exports[`number > 1211 > romanNumeral > with only min 1`] = `"MMMDCCXIV"`;

exports[`number > 1337 > bigInt > noArgs 1`] = `212435297136194n`;
exports[`number > 1337 > bigInt > noArgs 1`] = `212435297136195n`;

exports[`number > 1337 > bigInt > with big options 1`] = `27379244885156992800029993n`;

exports[`number > 1337 > bigInt > with big options 1`] = `27379244885156992800029992n`;
exports[`number > 1337 > bigInt > with bigint value 1`] = `90n`;

exports[`number > 1337 > bigInt > with bigint value 1`] = `88n`;
exports[`number > 1337 > bigInt > with boolean value 1`] = `1n`;

exports[`number > 1337 > bigInt > with boolean value 1`] = `0n`;
exports[`number > 1337 > bigInt > with multipleOf 1`] = `212435297138188n`;

exports[`number > 1337 > bigInt > with number value 1`] = `21n`;
exports[`number > 1337 > bigInt > with number value 1`] = `22n`;

exports[`number > 1337 > bigInt > with options 1`] = `58n`;
exports[`number > 1337 > bigInt > with options 1`] = `60n`;

exports[`number > 1337 > bigInt > with string value 1`] = `21n`;
exports[`number > 1337 > bigInt > with string value 1`] = `22n`;

exports[`number > 1337 > binary > noArgs 1`] = `"0"`;

Expand Down
30 changes: 29 additions & 1 deletion test/modules/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('number', () => {
.it('with boolean value', true)
.it('with bigint value', 123n)
.it('with options', { min: -42, max: 69 })
.it('with multipleOf', { multipleOf: 7919n })
.it('with big options', {
min: 6135715171537515454317351n,
max: 32465761264574654845432354n,
Expand Down Expand Up @@ -631,7 +632,34 @@ describe('number', () => {
expect(() => {
faker.number.bigInt({ min, max });
}).toThrow(
new FakerError(`Max ${max} should be larger then min ${min}.`)
new FakerError(`Max ${max} should be larger than min ${min}.`)
);
});

it('should generate a random bigint with a given multipleOf of 1n', () => {
const generateBigInt = faker.number.bigInt({ multipleOf: 1n });
expect(generateBigInt).toBeTypeOf('bigint');
});

it('should generate a random bigint with a given multipleOf of 7919n', () => {
const generateBigInt = faker.number.bigInt({ multipleOf: 7919n });
expect(generateBigInt).toBeTypeOf('bigint');
expect(generateBigInt % 7919n).toBe(0n);
});

it('should throw for non-positive multipleOf', () => {
expect(() => faker.number.bigInt({ multipleOf: 0n })).toThrow(
new FakerError('multipleOf should be greater than 0n.')
);
});

it('should throw when multipleOf is larger than the given max', () => {
expect(() =>
faker.number.bigInt({ max: 10n, multipleOf: 20n })
).toThrow(
new FakerError(
'Multiple of 20n should be less than or equal to max 10n.'
)
);
});
});
Expand Down