feat(number): add distributor functions#3375
Conversation
✅ Deploy Preview for fakerjs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## next #3375 +/- ##
==========================================
+ Coverage 98.83% 98.90% +0.06%
==========================================
Files 894 896 +2
Lines 3086 3100 +14
Branches 548 556 +8
==========================================
+ Hits 3050 3066 +16
+ Misses 32 30 -2
Partials 4 4
🚀 New features to boost your workflow:
|
|
I'm also not sure whether |
|
I don't like calling it "exponentialDistribution " because you're not returning a distribution, you are returning a single value from that distribution. So I'd call it exponentialValue or floatExponential or something similar to emphasise the thing that is returned is a float or a value. |
I'm not even sure that is the case.
Good idea. I'm not finally sure which term I would like to use but stepping away from calling it I'm currently considering one of these:
|
|
Team decision The feature will be implemented by extending the already existing methods ( We are currently unsure how to name the parameter. While distribution is okay, we are not sure if this is the mathmatical correct term. |
|
Ready for review. I changed the implementations to use a |
There was a problem hiding this comment.
Pull request overview
Adds a new “distributor” concept to influence randomness distribution for faker.number.int() / faker.number.float(), and publishes built-in distributor helpers with matching API docs support.
Changes:
- Add
distributoroption toNumberModule.int()andNumberModule.float()(defaulting to uniform). - Introduce
Distributortype plusuniformDistributorandexponentialDistributor, exported from the package entrypoint. - Extend API docs generation/rendering to include a new “Distributors” page and support table output + refreshable distributor calls in examples.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| test/scripts/apidocs/snapshots/verify-jsdoc-tags.spec.ts.snap | Snapshot update to include new distributors docs section. |
| test/modules/number.spec.ts | Adds distribution-behavior tests for exponentialDistributor with int/float. |
| src/modules/number/index.ts | Adds distributor option to int/float and uses it to derive the underlying random float. |
| src/index.ts | Re-exports Distributor, uniformDistributor, and exponentialDistributor. |
| src/distributors/distributor.ts | Introduces the Distributor type and its documentation. |
| src/distributors/uniform.ts | Adds the uniform distributor helper. |
| src/distributors/exponential.ts | Adds the exponential/power-law distributor helper with base/bias configuration. |
| scripts/shared/refreshable-code.ts | Records distributor(...) calls in doc examples for “refreshable code”. |
| scripts/shared/markdown.ts | Allows table tags/attributes in sanitized HTML and adds wrapCode() helper. |
| scripts/apidocs/processing/method.ts | Adds discovery/processing of distributor functions for API docs. |
| scripts/apidocs/processing/class.ts | Adds “Distributors” API docs page generation. |
| scripts/apidocs/generate.ts | Includes distributors page in docs generation pipeline. |
| docs/.vitepress/components/api-docs/refreshable-code.vue | Updates recorded-invocation detection to include distributor(...). |
| docs/.vitepress/api-pages.ts | Adds “Distributors” to the API sidebar. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
xDivisionByZerox
left a comment
There was a problem hiding this comment.
Other than the comment in #3375 (comment) I couldn't find anything that is blocking from my side. After I got some other opinions on the matter, I'm feeling confident in approving this.
Fixed |
|
Thank you for bringing this to an end @ST-DDT! 🙏 |
Implements #1862
Adds a
distributoroption to faker.number.int and float. The distributor function can be used to influence the distribution of the generated values. E.g. uniformDistributor and exponentialDistributor.The following table shows the rough distribution of values generated using
exponentialDistributor({ base: x }):The exponential distribution is achieved by using
base ** exponentwhere exponent is a random float between 0 and 1.The result is then stretched evenly to fit to
minandmax.