Skip to content

Commit

Permalink
refactor(number): replace precision with fractionDigits
Browse files Browse the repository at this point in the history
  • Loading branch information
xDivisionByZerox committed Feb 17, 2023
1 parent d1de875 commit cb35103
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 109 deletions.
18 changes: 9 additions & 9 deletions src/modules/color/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export class ColorModule {

color = Array.from({ length: 3 }, () => this.faker.number.int(255));
if (includeAlpha) {
color.push(this.faker.number.float({ precision: 0.01 }));
color.push(this.faker.number.float({ fractionDigits: 2 }));
cssFunction = 'rgba';
}

Expand Down Expand Up @@ -460,7 +460,7 @@ export class ColorModule {
}): string | number[];
cmyk(options?: { format?: ColorFormat }): string | number[] {
const color: string | number[] = Array.from({ length: 4 }, () =>
this.faker.number.float({ precision: 0.01 })
this.faker.number.float({ fractionDigits: 2 })
);
return toColorFormat(color, options?.format || 'decimal', 'cmyk');
}
Expand Down Expand Up @@ -570,7 +570,7 @@ export class ColorModule {
}): string | number[] {
const hsl: number[] = [this.faker.number.int(360)];
for (let i = 0; i < (options?.includeAlpha ? 3 : 2); i++) {
hsl.push(this.faker.number.float({ precision: 0.01 }));
hsl.push(this.faker.number.float({ fractionDigits: 2 }));
}

return toColorFormat(
Expand Down Expand Up @@ -676,7 +676,7 @@ export class ColorModule {
}): string | number[] {
const hsl: number[] = [this.faker.number.int(360)];
for (let i = 0; i < 2; i++) {
hsl.push(this.faker.number.float({ precision: 0.01 }));
hsl.push(this.faker.number.float({ fractionDigits: 2 }));
}

return toColorFormat(hsl, options?.format || 'decimal', 'hwb');
Expand Down Expand Up @@ -755,10 +755,10 @@ export class ColorModule {
format?: ColorFormat;
}): string | number[];
lab(options?: { format?: ColorFormat }): string | number[] {
const lab = [this.faker.number.float({ precision: 0.000001 })];
const lab = [this.faker.number.float({ fractionDigits: 6 })];
for (let i = 0; i < 2; i++) {
lab.push(
this.faker.number.float({ min: -100, max: 100, precision: 0.0001 })
this.faker.number.float({ min: -100, max: 100, fractionDigits: 4 })
);
}

Expand Down Expand Up @@ -850,9 +850,9 @@ export class ColorModule {
format?: ColorFormat;
}): string | number[];
lch(options?: { format?: ColorFormat }): string | number[] {
const lch = [this.faker.number.float({ precision: 0.000001 })];
const lch = [this.faker.number.float({ fractionDigits: 6 })];
for (let i = 0; i < 2; i++) {
lch.push(this.faker.number.float({ max: 230, precision: 0.1 }));
lch.push(this.faker.number.float({ max: 230, fractionDigits: 1 }));
}

return toColorFormat(lch, options?.format || 'decimal', 'lch');
Expand Down Expand Up @@ -960,7 +960,7 @@ export class ColorModule {
}

const color = Array.from({ length: 3 }, () =>
this.faker.number.float({ precision: 0.0001 })
this.faker.number.float({ fractionDigits: 4 })
);
return toColorFormat(
color,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/finance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ export class FinanceModule {
const randValue = this.faker.number.float({
max,
min,
precision: 10 ** -dec,
fractionDigits: dec,
});

let formattedString: string;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ export class HelpersModule {
const random = this.faker.number.float({
min: 0,
max: total,
precision: 1e-9,
fractionDigits: 9,
});
let current = 0;
for (const { weight, value } of array) {
Expand Down
8 changes: 4 additions & 4 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ export class LocationModule {

const { max = 90, min = legacyMin, precision = legacyPrecision } = options;

return this.faker.number.float({ min, max, precision: 10 ** -precision });
return this.faker.number.float({ min, max, fractionDigits: precision });
}

/**
Expand Down Expand Up @@ -629,7 +629,7 @@ export class LocationModule {

const { max = 180, min = legacyMin, precision = legacyPrecision } = options;

return this.faker.number.float({ max, min, precision: 10 ** -precision });
return this.faker.number.float({ max, min, fractionDigits: precision });
}

/**
Expand Down Expand Up @@ -873,15 +873,15 @@ export class LocationModule {

const angleRadians = this.faker.number.float({
max: 2 * Math.PI,
precision: 0.00001,
fractionDigits: 5,
}); // in ° radians

const radiusMetric = isMetric ? radius : radius * 1.60934; // in km
const errorCorrection = 0.995; // avoid float issues
const distanceInKm =
this.faker.number.float({
max: radiusMetric,
precision: 0.001,
fractionDigits: 3,
}) * errorCorrection; // in km

/**
Expand Down
43 changes: 20 additions & 23 deletions src/modules/number/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,18 @@ export class NumberModule {
* @param options Upper bound or options object. Defaults to `{}`.
* @param options.min Lower bound for generated number. Defaults to `0.0`.
* @param options.max Upper bound for generated number. Defaults to `1.0`.
* @param options.precision Precision of the generated number, for example `0.01` will round to 2 decimal points.
* @param options.fractionDigits The number of digits to appear after the decimal point. Defaults to `16`.
*
* @throws If options.max is smaller than options.min.
* @throws If options.fractionDigits is negative.
*
* @example
* faker.number.float() // 0.5688541042618454
* faker.number.float(3) // 2.367973240558058
* faker.number.float({ min: -1000000 }) //-780678.849672846
* faker.number.float({ max: 100 }) // 17.3687307164073
* faker.number.float({ precision: 0.1 }) // 0.9
* faker.number.float({ min: 10, max: 100, precision: 0.001 }) // 35.415
* faker.number.float({ fractionDigits: 1 }) // 0.9
* faker.number.float({ min: 10, max: 100, v: 3 }) // 35.415
*
* @since 8.0.0
*/
Expand All @@ -122,11 +125,11 @@ export class NumberModule {
*/
max?: number;
/**
* Precision of the generated number.
* The number of digits to appear after the decimal point.
*
* @default 0.01
* @default 16
*/
precision?: number;
fractionDigits?: number;
} = {}
): number {
if (typeof options === 'number') {
Expand All @@ -135,7 +138,7 @@ export class NumberModule {
};
}

const { min = 0, max = 1, precision } = options;
const { min = 0, max = 1, fractionDigits = 16 } = options;

if (max === min) {
return min;
Expand All @@ -145,23 +148,17 @@ export class NumberModule {
throw new FakerError(`Max ${max} should be greater than min ${min}.`);
}

if (precision !== undefined) {
if (precision <= 0) {
throw new FakerError(`Precision should be greater than 0.`);
}

const factor = 1 / precision;
const int = this.int({
min: min * factor,
max: max * factor,
});
return int / factor;
} else {
// @ts-expect-error: access private member field
const mersenne: Mersenne = this.faker._mersenne;
const real = mersenne.next();
return real * (max - min) + min;
if (fractionDigits < 0) {
throw new FakerError(
'The fractional digits count should be greater than 0.'
);
}

// @ts-expect-error: access private member field
const mersenne: Mersenne = this.faker._mersenne;
const real = mersenne.next() * (max - min) + min;

return parseFloat(real.toFixed(fractionDigits));
}

/**
Expand Down
30 changes: 15 additions & 15 deletions test/__snapshots__/color.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ exports[`color > 42 > cmyk 1`] = `
[
0.37,
0.8,
0.96,
0.95,
0.18,
]
`;

exports[`color > 42 > colorByCSSColorSpace 1`] = `
[
0.3745,
0.7966,
0.9508,
0.7965,
0.9507,
]
`;

Expand All @@ -25,7 +25,7 @@ exports[`color > 42 > hsl 1`] = `
[
135,
0.8,
0.96,
0.95,
]
`;

Expand All @@ -35,7 +35,7 @@ exports[`color > 42 > hwb 1`] = `
[
135,
0.8,
0.96,
0.95,
]
`;

Expand Down Expand Up @@ -63,14 +63,14 @@ exports[`color > 1211 > cmyk 1`] = `
[
0.93,
0.46,
0.9,
0.89,
0.78,
]
`;

exports[`color > 1211 > colorByCSSColorSpace 1`] = `
[
0.9286,
0.9285,
0.459,
0.8935,
]
Expand All @@ -84,7 +84,7 @@ exports[`color > 1211 > hsl 1`] = `
[
335,
0.46,
0.9,
0.89,
]
`;

Expand All @@ -94,21 +94,21 @@ exports[`color > 1211 > hwb 1`] = `
[
335,
0.46,
0.9,
0.89,
]
`;

exports[`color > 1211 > lab 1`] = `
[
0.928521,
0.92852,
-8.197,
78.6944,
78.6943,
]
`;

exports[`color > 1211 > lch 1`] = `
[
0.928521,
0.92852,
105.6,
205.5,
]
Expand All @@ -131,7 +131,7 @@ exports[`color > 1337 > colorByCSSColorSpace 1`] = `
[
0.262,
0.5605,
0.1586,
0.1587,
]
`;

Expand Down Expand Up @@ -159,15 +159,15 @@ exports[`color > 1337 > hwb 1`] = `

exports[`color > 1337 > lab 1`] = `
[
0.262024,
0.262025,
12.106,
-68.2632,
]
`;

exports[`color > 1337 > lch 1`] = `
[
0.262024,
0.262025,
128.9,
36.5,
]
Expand Down
18 changes: 9 additions & 9 deletions test/__snapshots__/finance.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ exports[`finance > 42 > amount > with min and max option 1`] = `"24.98"`;

exports[`finance > 42 > amount > with min option 1`] = `"380.79"`;

exports[`finance > 42 > amount > with min, leagcy max, leagcy dec and leagcy symbol 1`] = `"$24.98161"`;
exports[`finance > 42 > amount > with min, leagcy max, leagcy dec and leagcy symbol 1`] = `"$24.98160"`;

exports[`finance > 42 > amount > with min, max and dec option 1`] = `"24.98161"`;
exports[`finance > 42 > amount > with min, max and dec option 1`] = `"24.98160"`;

exports[`finance > 42 > amount > with min, max, dec and symbol option 1`] = `"#24.98161"`;
exports[`finance > 42 > amount > with min, max, dec and symbol option 1`] = `"#24.98160"`;

exports[`finance > 42 > amount > with min, max, dec, symbol and autoFormat option 1`] = `"#24.98161"`;
exports[`finance > 42 > amount > with min, max, dec, symbol and autoFormat option 1`] = `"#24.98160"`;

exports[`finance > 42 > bic > noArgs 1`] = `"UYETSCLLG53"`;

Expand Down Expand Up @@ -102,15 +102,15 @@ exports[`finance > 1211 > accountName 1`] = `"Personal Loan Account"`;

exports[`finance > 1211 > amount > noArgs 1`] = `"928.52"`;

exports[`finance > 1211 > amount > with leagcy dec 1`] = `"928.52016"`;
exports[`finance > 1211 > amount > with leagcy dec 1`] = `"928.52015"`;

exports[`finance > 1211 > amount > with leagcy max 1`] = `"46.43"`;

exports[`finance > 1211 > amount > with min 1`] = `"929.24"`;
exports[`finance > 1211 > amount > with min 1`] = `"929.23"`;

exports[`finance > 1211 > amount > with min and max option 1`] = `"47.15"`;
exports[`finance > 1211 > amount > with min and max option 1`] = `"47.14"`;

exports[`finance > 1211 > amount > with min option 1`] = `"929.24"`;
exports[`finance > 1211 > amount > with min option 1`] = `"929.23"`;

exports[`finance > 1211 > amount > with min, leagcy max, leagcy dec and leagcy symbol 1`] = `"$47.14081"`;

Expand Down Expand Up @@ -194,7 +194,7 @@ exports[`finance > 1337 > accountName 1`] = `"Money Market Account"`;

exports[`finance > 1337 > amount > noArgs 1`] = `"262.02"`;

exports[`finance > 1337 > amount > with leagcy dec 1`] = `"262.02467"`;
exports[`finance > 1337 > amount > with leagcy dec 1`] = `"262.02468"`;

exports[`finance > 1337 > amount > with leagcy max 1`] = `"13.10"`;

Expand Down

0 comments on commit cb35103

Please sign in to comment.