-
-
Notifications
You must be signed in to change notification settings - Fork 943
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): support step on faker.number.int() #2188
Conversation
@@ -91,11 +97,17 @@ export class NumberModule { | |||
throw new FakerError(`Max ${max} should be greater than min ${min}.`); | |||
} | |||
|
|||
if (step <= 0) { | |||
throw new FakerError(`Step should be a positive number.`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also throw on a noninteger step?
const range = (effectiveMax - effectiveMin) / step; | ||
const rand = Math.floor(real * (range + 1)); | ||
return effectiveMin + rand * step; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not work correctly.
For example faker.number.int({min:0, max:5, step: 2})
sometimes returns 6.
range is (5 - 0)/2 = 2.5
rand is say floor(0.99 * 3.5) = 3
returns 0 + 3 * 2 = 6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also per #1855 (comment)
faker.number.int({min:5, max: 100, step: 10})
should return {10, 20, ...} not {5, 15, 25...}
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## next #2188 +/- ##
=======================================
Coverage 99.59% 99.59%
=======================================
Files 2607 2607
Lines 245029 245041 +12
Branches 1151 1152 +1
=======================================
+ Hits 244034 244046 +12
Misses 968 968
Partials 27 27
|
Marked this for |
@@ -163,6 +163,22 @@ describe('number', () => { | |||
new FakerError(`No integer value between 2.1 and 2.9 found.`) | |||
); | |||
}); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line 9 should be extracted and separated together with a new it
line
Closed in favor of #2586 |
Fixes #2186
In faker 7 I could specify
But with faker 8, the precision was removed from int.
Suggested solution
Allow passing a step to the int faker function