aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2025-04-11 20:35:21 +0200
committerGitHub <[email protected]>2025-04-11 20:35:21 +0200
commite4cc4e50d1d4103c26f06fd2db0ca187dbb537cd (patch)
tree69b87c8165fe618278b68c87ab26552d01601753
parentf70a6f7a656090ed900ea18b39961d0d5ff78be0 (diff)
downloadfaker-e4cc4e50d1d4103c26f06fd2db0ca187dbb537cd.tar.xz
faker-e4cc4e50d1d4103c26f06fd2db0ca187dbb537cd.zip
fix(number): don't ignore multipleOf in float when min=max (#3417)
-rw-r--r--src/modules/number/index.ts6
-rw-r--r--test/modules/number.spec.ts38
2 files changed, 39 insertions, 5 deletions
diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts
index f1dce04f..9381f000 100644
--- a/src/modules/number/index.ts
+++ b/src/modules/number/index.ts
@@ -113,7 +113,7 @@ export class NumberModule extends SimpleModuleBase {
* @param options.fractionDigits The maximum number of digits to appear after the decimal point, for example `2` will round to 2 decimal points. Only one of `multipleOf` or `fractionDigits` should be passed.
*
* @throws When `min` is greater than `max`.
- * @throws When `multipleOf` is negative.
+ * @throws When `multipleOf` is not a positive number.
* @throws When `fractionDigits` is negative.
* @throws When `fractionDigits` and `multipleOf` is passed in the same options object.
*
@@ -170,10 +170,6 @@ export class NumberModule extends SimpleModuleBase {
multipleOf = fractionDigits == null ? undefined : 10 ** -fractionDigits,
} = options;
- if (max === min) {
- return min;
- }
-
if (max < min) {
throw new FakerError(`Max ${max} should be greater than min ${min}.`);
}
diff --git a/test/modules/number.spec.ts b/test/modules/number.spec.ts
index 278baadb..60781af8 100644
--- a/test/modules/number.spec.ts
+++ b/test/modules/number.spec.ts
@@ -156,6 +156,18 @@ describe('number', () => {
);
});
+ it('throws for impossible multipleOf where min=max', () => {
+ const input = {
+ min: 11,
+ max: 11,
+ multipleOf: 10,
+ };
+
+ expect(() => faker.number.int(input)).toThrow(
+ new FakerError('No suitable integer value between 11 and 11 found.')
+ );
+ });
+
it('should return a random number given a maximum value as Number', () => {
const actual = faker.number.int(10);
@@ -391,6 +403,32 @@ describe('number', () => {
);
});
+ it('throws for impossible multipleOf', () => {
+ const input = {
+ min: 11,
+ max: 19,
+ multipleOf: 10,
+ };
+
+ expect(() => faker.number.float(input)).toThrow(
+ new FakerError(
+ 'No suitable integer value between 1.1 and 1.9000000000000001 found.'
+ )
+ );
+ });
+
+ it('throws for impossible multipleOf where min=max', () => {
+ const input = {
+ min: 11,
+ max: 11,
+ multipleOf: 10,
+ };
+
+ expect(() => faker.number.float(input)).toThrow(
+ new FakerError('No suitable integer value between 1.1 and 1.1 found.')
+ );
+ });
+
it('should not modify the input object', () => {
expect(() =>
faker.number.float(Object.freeze({ min: 1, max: 2 }))