Skip to content

Commit

Permalink
fix(number): values out of bounds (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Dec 10, 2022
1 parent e181150 commit e4839a9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
15 changes: 11 additions & 4 deletions src/modules/number/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,27 @@ export class NumberModule {
}

const { min = 0, max = min + 99999 } = options;
const effectiveMin = Math.ceil(min);
const effectiveMax = Math.floor(max);

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

if (max < min) {
if (effectiveMax < effectiveMin) {
if (max >= min) {
throw new FakerError(
`No integer value between ${min} and ${max} found.`
);
}
throw new FakerError(`Max ${max} should be greater than min ${min}.`);
}

const mersenne: Mersenne =
// @ts-expect-error: access private member field
this.faker._mersenne;

return mersenne.next({ min, max: max + 1 });
return mersenne.next({ min: effectiveMin, max: effectiveMax + 1 });
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/finance.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports[`finance > 42 > amount > with max 1`] = `"18.73"`;

exports[`finance > 42 > amount > with min 1`] = `"380.79"`;

exports[`finance > 42 > amount > with min and max and dec and symbol 1`] = `"$24.98160"`;
exports[`finance > 42 > amount > with min and max and dec and symbol 1`] = `"$24.98161"`;

exports[`finance > 42 > bic > noArgs 1`] = `"UYETSCLLG53"`;

Expand Down Expand Up @@ -146,7 +146,7 @@ exports[`finance > 1337 > amount > with max 1`] = `"13.10"`;

exports[`finance > 1337 > amount > with min 1`] = `"269.40"`;

exports[`finance > 1337 > amount > with min and max and dec and symbol 1`] = `"$20.48098"`;
exports[`finance > 1337 > amount > with min and max and dec and symbol 1`] = `"$20.48099"`;

exports[`finance > 1337 > bic > noArgs 1`] = `"OEFHLYG18IL"`;

Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/location.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ exports[`location > 1211 > longitude > noArgs 1`] = `154.2673`;

exports[`location > 1211 > nearbyGPSCoordinate > near origin 1`] = `
[
-0.02872051646443488,
0.05959053473372933,
-0.02872111236834616,
0.05959024752564801,
]
`;

Expand Down
19 changes: 8 additions & 11 deletions test/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,13 @@ describe('number', () => {
}).toThrowError(`Max ${max} should be greater than min ${min}.`);
});

// TODO @Shinigami92 2022-11-24: https://github.com/faker-js/faker/issues/1595
it.todo(
'should throw when there is no integer between min and max',
() => {
expect(() => {
faker.number.int({ min: 2.1, max: 2.9 });
}).toThrow();
}
);
it('should throw when there is no integer between min and max', () => {
expect(() => {
faker.number.int({ min: 2.1, max: 2.9 });
}).toThrow(
new FakerError(`No integer value between 2.1 and 2.9 found.`)
);
});
});

describe('float', () => {
Expand Down Expand Up @@ -193,8 +191,7 @@ describe('number', () => {
expect(results).toEqual([0, 0.5, 1, 1.5]);
});

// TODO @Shinigami92 2022-11-24: https://github.com/faker-js/faker/issues/1595
it.todo('provides numbers with a given precision of 0.4 steps', () => {
it('provides numbers with a given precision of 0.4 steps', () => {
const results = Array.from(
new Set(
Array.from({ length: 50 }, () =>
Expand Down

0 comments on commit e4839a9

Please sign in to comment.