-
-
Notifications
You must be signed in to change notification settings - Fork 941
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: precise number calculation #779
Conversation
Codecov Report
@@ Coverage Diff @@
## main #779 +/- ##
=======================================
Coverage 99.34% 99.34%
=======================================
Files 1922 1922
Lines 183057 183064 +7
Branches 903 904 +1
=======================================
+ Hits 181860 181867 +7
Misses 1141 1141
Partials 56 56
|
It looks like that this is not healthy and when using somehow e.g. rollup to bundle your app it fails So instead of using a We could e.g. do this: import { faker } from '@faker-js/faker'
import Decimal from 'decimal.js'
faker.Decimal = Decimal Or maybe even make it more general like import { faker } from '@faker-js/faker'
import Decimal from 'decimal.js'
faker.datatype.number({
min,
max,
precision: 0.00001,
precisionAlgo: (precision) => new Decimal(1)
.dividedBy(precision)
.toNumber()
}) But this has the problem that it could drastically compromise the value by just using a wrong implementation. |
We should not pollute our public API like this.
Hard no. |
src/datatype.ts
Outdated
import type { Faker } from '.'; | ||
import { deprecated } from './internal/deprecated'; | ||
|
||
/** | ||
* Module to generate various primitive values and data types. | ||
*/ | ||
export class Datatype { | ||
private Decimal: typeof Decimal | null = null; |
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.
IMO this should be a function (value, precision) => number
or just a divide/multiply function.
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.
So yeah, like a combination of both my suggestions, but at module-scope instead of faker-scope
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.
@internal
or private
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.
@internal
orprivate
No, non of these, the user needs to be able optionally provide they own algorithm. That's the point of this PR.
Please have a look into my newest commit.
@@ -5,6 +5,10 @@ import { deprecated } from './internal/deprecated'; | |||
* Module to generate various primitive values and data types. | |||
*/ | |||
export class Datatype { | |||
public preciseNumberDivider: |
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.
Does someone have a better name?
I also need to declare and use it like this way, because if I eagerly provide a default function, it counts as datatype module function and will be bound by the constructor and the all_functional test fails badly.
Currently looks like there is no much interest in this PR, will close it for now |
closes #331
supersedes #555