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

fix: decimal javascript calculation #555

Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"c8": "~7.11.0",
"conventional-changelog-cli": "~2.2.2",
"cypress": "~9.5.0",
"decimal.js": "^10.3.1",
"esbuild": "~0.14.23",
"eslint": "~8.9.0",
"eslint-config-prettier": "~8.4.0",
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions src/datatype.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { Faker } from '.';
import Decimal from 'decimal.js';

/**
* Module to generate various primitive values and data types.
*/
export class Datatype {
constructor(private readonly faker: Faker, seed?: any[] | any) {
constructor(private readonly faker: Faker, seed?: number[] | number) {
// Use a user provided seed if it is an array or number
if (Array.isArray(seed) && seed.length) {
this.faker.mersenne.seed_array(seed);
} else if (!isNaN(seed)) {
this.faker.mersenne.seed(seed);
} else if (!isNaN(seed as number)) {
this.faker.mersenne.seed(seed as number);
}

// Bind `this` so namespaced is working correctly
Expand Down Expand Up @@ -72,7 +73,10 @@ export class Datatype {
)
);
// Workaround problem in Float point arithmetics for e.g. 6681493 / 0.01
randomNumber = randomNumber / (1 / options.precision);
const decimalPrecision = new Decimal(1)
.dividedBy(options?.precision)
.toNumber();
randomNumber = randomNumber / decimalPrecision;

return randomNumber;
}
Expand Down Expand Up @@ -275,7 +279,7 @@ export class Datatype {
? this.faker.datatype.string()
: this.faker.datatype.number();
}
return returnArray;
return returnArray as (string | number)[];
}

/**
Expand Down
48 changes: 48 additions & 0 deletions test/datatype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const seededRuns = [
withMinAndMax: -1,
withMax: 26,
withMinAndMaxAndPrecision: -0.43,
withMinAndMaxAndPrecision5: -0.42605,
withMinAndMaxAndPrecision9: -0.426047312,
},
float: {
noArgs: 37453.64,
Expand All @@ -20,6 +22,8 @@ const seededRuns = [
withMinAndMax: -0.43,
withMax: 25.84,
withMinAndMaxAndPrecision: -0.4261,
withMinAndMaxAndPrecision5: -0.42605,
withMinAndMaxAndPrecision9: -0.426047312,
},
datetime: {
// TODO @Shinigami92 2022-01-29: We will fix the deterministic in #343
Expand Down Expand Up @@ -81,6 +85,8 @@ const seededRuns = [
withMinAndMax: -13,
withMax: 18,
withMinAndMaxAndPrecision: -12.92,
withMinAndMaxAndPrecision5: -12.91526,
withMinAndMaxAndPrecision9: -12.915260943,
},
float: {
noArgs: 26202.2,
Expand All @@ -89,6 +95,8 @@ const seededRuns = [
withMinAndMax: -12.92,
withMax: 18.08,
withMinAndMaxAndPrecision: -12.9153,
withMinAndMaxAndPrecision5: -12.91526,
withMinAndMaxAndPrecision9: -12.915260943,
},
datetime: {
// TODO @Shinigami92 2022-01-29: We will fix the deterministic in #343
Expand Down Expand Up @@ -150,6 +158,8 @@ const seededRuns = [
withMinAndMax: 61,
withMax: 64,
withMinAndMaxAndPrecision: 61.07,
withMinAndMaxAndPrecision5: 61.06574,
withMinAndMaxAndPrecision9: 61.065737066,
},
float: {
noArgs: 92851.09,
Expand All @@ -158,6 +168,8 @@ const seededRuns = [
withMinAndMax: 61.07,
withMax: 64.07,
withMinAndMaxAndPrecision: 61.0658,
withMinAndMaxAndPrecision5: 61.06574,
withMinAndMaxAndPrecision9: 61.065737066,
},
datetime: {
// TODO @Shinigami92 2022-01-29: We will fix the deterministic in #343
Expand Down Expand Up @@ -290,6 +302,32 @@ describe('datatype', () => {
});
expect(actual).toEqual(expectations.number.withMinAndMaxAndPrecision);
});

it('should return a deterministic value for given min, max and precision of 0.00001', () => {
faker.seed(seed);

const actual = faker.datatype.number({
min: -42,
max: 69,
precision: 0.00001,
});
expect(actual).toEqual(
expectations.number.withMinAndMaxAndPrecision5
);
});

it('should return a deterministic value for given min, max and precision of 0.000000001', () => {
faker.seed(seed);

const actual = faker.datatype.number({
min: -42,
max: 69,
precision: 0.000000001,
});
expect(actual).toEqual(
expectations.number.withMinAndMaxAndPrecision9
);
});
});

describe('float', () => {
Expand Down Expand Up @@ -469,6 +507,16 @@ describe('datatype', () => {
expect(number).toBe(Number(number.toFixed(3)));
});

it('should return a random float given a precision value of 0.00001', () => {
const number = faker.datatype.float(0.00001);
expect(number).toBe(Number(number.toFixed(5)));
});

it('should return a random float given a precision value of 0.000000001', () => {
const number = faker.datatype.float(0.000000001);
expect(number).toBe(Number(number.toFixed(9)));
});

it('should return a random number given a maximum value as Object', () => {
const options = { max: 10 };
expect(faker.datatype.float(options)).greaterThanOrEqual(0);
Expand Down