Skip to content
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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
"eslint --ext .ts"
]
},
"peerDependencies": {
"decimal.js": "~10.3.1"
},
"peerDependenciesMeta": {
"decimal.js": {
"optional": true
}
},
"devDependencies": {
"@types/markdown-it": "~12.2.3",
"@types/node": "~16.11.26",
Expand Down
20 changes: 20 additions & 0 deletions src/datatype.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// @ts-ignore: decimal.js is an optional peer dependency
import type { Decimal } from 'decimal.js';
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;
Copy link
Member

@ST-DDT ST-DDT Apr 5, 2022

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.

Copy link
Member Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@internal or private

Copy link
Member Author

@Shinigami92 Shinigami92 Apr 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@internal or private

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.


constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(Datatype.prototype)) {
Expand All @@ -13,6 +17,15 @@ export class Datatype {
}
this[name] = this[name].bind(this);
}

// @ts-ignore: decimal.js is an optional peer dependency
import('decimal.js')
.then((module) => {
this.Decimal = module.default;
})
.catch(() => {
// Ignore
});
}

/**
Expand Down Expand Up @@ -57,6 +70,13 @@ export class Datatype {
);

// Workaround problem in float point arithmetics for e.g. 6681493 / 0.01
if (this.Decimal) {
const decimalPrecision = new this.Decimal(1)
.dividedBy(precision)
.toNumber();
return randomNumber / decimalPrecision;
}

return randomNumber / (1 / precision);
}

Expand Down