From fc14509c5473ee8a0a9834da7341e3f4af1f623c Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 5 Apr 2022 15:07:15 +0200 Subject: [PATCH 1/3] feat: precise number calculation --- package.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/package.json b/package.json index 54aec8f7778..1095255bba1 100644 --- a/package.json +++ b/package.json @@ -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", From 2b77458895b0e0a9580077b02f2ba43db9c3a994 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 5 Apr 2022 15:59:06 +0200 Subject: [PATCH 2/3] feat: precise number calculation --- src/datatype.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/datatype.ts b/src/datatype.ts index 50bcb17fda8..5037751466b 100644 --- a/src/datatype.ts +++ b/src/datatype.ts @@ -1,3 +1,5 @@ +// @ts-ignore: decimal.js is an optional peer dependency +import type { Decimal } from 'decimal.js'; import type { Faker } from '.'; import { deprecated } from './internal/deprecated'; @@ -5,6 +7,8 @@ import { deprecated } from './internal/deprecated'; * Module to generate various primitive values and data types. */ export class Datatype { + private Decimal: typeof Decimal | null = null; + constructor(private readonly faker: Faker) { // Bind `this` so namespaced is working correctly for (const name of Object.getOwnPropertyNames(Datatype.prototype)) { @@ -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 + }); } /** @@ -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); } From 882920a6e9cd6a3dbe14d6c8ead61e69874cf352 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 5 Apr 2022 16:48:55 +0200 Subject: [PATCH 3/3] feat: precise number calculation --- package.json | 8 -------- src/datatype.ts | 29 ++++++++--------------------- 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 1095255bba1..54aec8f7778 100644 --- a/package.json +++ b/package.json @@ -87,14 +87,6 @@ "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", diff --git a/src/datatype.ts b/src/datatype.ts index 5037751466b..837239309ff 100644 --- a/src/datatype.ts +++ b/src/datatype.ts @@ -1,5 +1,3 @@ -// @ts-ignore: decimal.js is an optional peer dependency -import type { Decimal } from 'decimal.js'; import type { Faker } from '.'; import { deprecated } from './internal/deprecated'; @@ -7,7 +5,9 @@ import { deprecated } from './internal/deprecated'; * Module to generate various primitive values and data types. */ export class Datatype { - private Decimal: typeof Decimal | null = null; + public preciseNumberDivider: + | ((value: number, precision: number) => number) + | null = null; constructor(private readonly faker: Faker) { // Bind `this` so namespaced is working correctly @@ -17,15 +17,6 @@ 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 - }); } /** @@ -69,15 +60,11 @@ export class Datatype { this.faker.mersenne.rand(max / precision, min / precision) ); - // 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); + return ( + this.preciseNumberDivider?.(randomNumber, precision) ?? + // Workaround problem in float point arithmetics for e.g. 6681493 / 0.01 + randomNumber / (1 / precision) + ); } /**