Skip to content

Commit

Permalink
refactor: compress multipleOf logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Mar 11, 2024
1 parent 8e93192 commit 633e6fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
40 changes: 15 additions & 25 deletions src/modules/number/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,48 +71,38 @@ export class NumberModule extends SimpleModuleBase {
}

const { min = 0, max = Number.MAX_SAFE_INTEGER, multipleOf = 1 } = options;
const effectiveMin = Math.ceil(min);
const effectiveMax = Math.floor(max);

if (!Number.isInteger(multipleOf)) {
throw new FakerError(`multipleOf should be an integer.`);
}

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

const effectiveMin = Math.ceil(min / multipleOf);
const effectiveMax = Math.floor(max / multipleOf);

if (effectiveMin === effectiveMax) {
return effectiveMin;
return effectiveMin * multipleOf;
}

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

throw new FakerError(`Max ${max} should be greater than min ${min}.`);
}

if (!Number.isInteger(multipleOf)) {
throw new FakerError(`multipleOf should be an integer.`);
}

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

// @ts-expect-error: access private member field
const randomizer = this.faker._randomizer;
const real = randomizer.next();
if (multipleOf > 1) {
const minMultiple = Math.ceil(effectiveMin / multipleOf) * multipleOf;
const maxMultiple = Math.floor(effectiveMax / multipleOf) * multipleOf;
if (maxMultiple < minMultiple) {
throw new FakerError(
`No suitable integer value between ${min} and ${max} found.`
);
}

const delta = (maxMultiple - minMultiple) / multipleOf + 1;
return Math.floor(real * delta) * multipleOf + minMultiple;
}
const delta = effectiveMax - effectiveMin + 1; // +1 for inclusive max bounds and even distribution

return Math.floor(real * (effectiveMax + 1 - effectiveMin) + effectiveMin);
return Math.floor(real * delta + effectiveMin) * multipleOf;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions test/modules/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describe('number', () => {
expect(() => {
faker.number.int({ min: 2.1, max: 2.9 });
}).toThrow(
new FakerError(`No integer value between 2.1 and 2.9 found.`)
new FakerError(`No suitable integer value between 2.1 and 2.9 found.`)
);
});
});
Expand Down Expand Up @@ -519,7 +519,7 @@ describe('number', () => {
expect(() => {
faker.number.binary({ min: 2.1, max: 2.9 });
}).toThrow(
new FakerError(`No integer value between 2.1 and 2.9 found.`)
new FakerError(`No suitable integer value between 2.1 and 2.9 found.`)
);
});
});
Expand Down Expand Up @@ -570,7 +570,7 @@ describe('number', () => {
expect(() => {
faker.number.octal({ min: 2.1, max: 2.9 });
}).toThrow(
new FakerError(`No integer value between 2.1 and 2.9 found.`)
new FakerError(`No suitable integer value between 2.1 and 2.9 found.`)
);
});
});
Expand Down Expand Up @@ -618,7 +618,7 @@ describe('number', () => {
expect(() => {
faker.number.hex({ min: 2.1, max: 2.9 });
}).toThrow(
new FakerError(`No integer value between 2.1 and 2.9 found.`)
new FakerError(`No suitable integer value between 2.1 and 2.9 found.`)
);
});
});
Expand Down

0 comments on commit 633e6fa

Please sign in to comment.