Skip to content

Commit e4cc4e5

Browse files
authored
fix(number): don't ignore multipleOf in float when min=max (#3417)
1 parent f70a6f7 commit e4cc4e5

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

src/modules/number/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class NumberModule extends SimpleModuleBase {
113113
* @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.
114114
*
115115
* @throws When `min` is greater than `max`.
116-
* @throws When `multipleOf` is negative.
116+
* @throws When `multipleOf` is not a positive number.
117117
* @throws When `fractionDigits` is negative.
118118
* @throws When `fractionDigits` and `multipleOf` is passed in the same options object.
119119
*
@@ -170,10 +170,6 @@ export class NumberModule extends SimpleModuleBase {
170170
multipleOf = fractionDigits == null ? undefined : 10 ** -fractionDigits,
171171
} = options;
172172

173-
if (max === min) {
174-
return min;
175-
}
176-
177173
if (max < min) {
178174
throw new FakerError(`Max ${max} should be greater than min ${min}.`);
179175
}

test/modules/number.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ describe('number', () => {
156156
);
157157
});
158158

159+
it('throws for impossible multipleOf where min=max', () => {
160+
const input = {
161+
min: 11,
162+
max: 11,
163+
multipleOf: 10,
164+
};
165+
166+
expect(() => faker.number.int(input)).toThrow(
167+
new FakerError('No suitable integer value between 11 and 11 found.')
168+
);
169+
});
170+
159171
it('should return a random number given a maximum value as Number', () => {
160172
const actual = faker.number.int(10);
161173

@@ -391,6 +403,32 @@ describe('number', () => {
391403
);
392404
});
393405

406+
it('throws for impossible multipleOf', () => {
407+
const input = {
408+
min: 11,
409+
max: 19,
410+
multipleOf: 10,
411+
};
412+
413+
expect(() => faker.number.float(input)).toThrow(
414+
new FakerError(
415+
'No suitable integer value between 1.1 and 1.9000000000000001 found.'
416+
)
417+
);
418+
});
419+
420+
it('throws for impossible multipleOf where min=max', () => {
421+
const input = {
422+
min: 11,
423+
max: 11,
424+
multipleOf: 10,
425+
};
426+
427+
expect(() => faker.number.float(input)).toThrow(
428+
new FakerError('No suitable integer value between 1.1 and 1.1 found.')
429+
);
430+
});
431+
394432
it('should not modify the input object', () => {
395433
expect(() =>
396434
faker.number.float(Object.freeze({ min: 1, max: 2 }))

0 commit comments

Comments
 (0)