-
-
Notifications
You must be signed in to change notification settings - Fork 919
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(helpers): add new faker.helpers.weightedArrayElement #1654
feat(helpers): add new faker.helpers.weightedArrayElement #1654
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## next #1654 +/- ##
==========================================
- Coverage 99.64% 99.64% -0.01%
==========================================
Files 2245 2245
Lines 240269 240318 +49
Branches 1069 1078 +9
==========================================
+ Hits 239407 239453 +46
- Misses 841 844 +3
Partials 21 21
|
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.
I think we should swap the weight and the value (weight first), because the weight is usually much shorter than the value making it easier to spot in longer lists. What do you/the others think?
[
[ 5, 'sunny' ],
[ 4, 'rainy' ],
[ 1, 'extremely hazardous blizard storm clouds' ]
]
[
[ 'sunny', 5 ],
[ 'rainy', 4 ],
[ 'extremely hazardous blizard storm clouds', 1 ]
]
Should we allow weight zero for temporarily disabled or dynamically weighted items or should we expect the user to remove it before passing it to our method? |
Co-authored-by: Shinigami <[email protected]>
Co-authored-by: Shinigami <[email protected]>
Co-authored-by: Shinigami <[email protected]>
i think allowing zeroes causes more confusion, for example what would you expect to happen if your array had three items, all with weight zero? |
All have same chance |
Intuitively yes, but that would require some extra code to deal with that case, which i think adds more complexity and chance for bugs? |
Co-authored-by: ST-DDT <[email protected]>
I note that |
Maybe open a new issue for that (max precision float)? |
The linter now complains about this. |
Head branch was pushed to by a user without write access
3164b38
Split from #1637
Adds a new function
faker.helpers.weightedArrayElement
which allows youto select a random element from an array, but weighted more to certain
elements than others. This will be useful for refactoring name patterns,
where for example you might want to have
FirstName Surname
chosen 90% of the time andFirstName Surname Suffix
chosen 10% of the time