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

refactor(number)!: remove v8 deprecated number parameter #2738

Merged
merged 3 commits into from
Mar 12, 2024
Merged
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
7 changes: 7 additions & 0 deletions docs/guide/upgrading_v9/2738.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Remove deprecated number parameter

Removed deprecated number parameter

| old | replacement |
| ----------------------------------- | ------------------------------------ |
| `faker.number.float({ precision })` | `faker.number.float({ multipleOf })` |
41 changes: 9 additions & 32 deletions src/modules/number/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
import { SimpleModuleBase } from '../../internal/module-base';

/**
Expand Down Expand Up @@ -90,13 +89,11 @@ export class NumberModule extends SimpleModuleBase {
*
* @param options Upper bound or options object.
* @param options.min Lower bound for generated number, inclusive. Defaults to `0.0`.
* @param options.max Upper bound for generated number, exclusive, unless `multipleOf`, `precision` or `fractionDigits` are passed. Defaults to `1.0`.
* @param options.precision Deprecated alias for `multipleOf`. Only one of `multipleOf`, `precision` or `fractionDigits` should be passed.
* @param options.multipleOf The generated number will be a multiple of this parameter. Only one of `multipleOf`, `precision` or `fractionDigits` should be passed.
* @param options.fractionDigits The maximum number of digits to appear after the decimal point, for example `2` will round to 2 decimal points. Only one of `multipleOf`, `precision` or `fractionDigits` should be passed.
* @param options.max Upper bound for generated number, exclusive, unless `multipleOf` or `fractionDigits` are passed. Defaults to `1.0`.
* @param options.multipleOf The generated number will be a multiple of this parameter. Only one of `multipleOf` or `fractionDigits` should be passed.
* @param options.fractionDigits The maximum number of digits to appear after the decimal point, for example `2` will round to 2 decimal points. Only one of `multipleOf` or `fractionDigits` should be passed.
*
* @throws When `min` is greater than `max`.
* @throws When `precision` is negative.
* @throws When `multipleOf` is negative.
* @throws When `fractionDigits` is negative.
* @throws When `fractionDigits` and `multipleOf` is passed in the same options object.
Expand Down Expand Up @@ -125,23 +122,17 @@ export class NumberModule extends SimpleModuleBase {
*/
min?: number;
/**
* Upper bound for generated number, exclusive, unless `multipleOf`, `precision` or `fractionDigits` are passed.
* Upper bound for generated number, exclusive, unless `multipleOf` or `fractionDigits` are passed.
*
* @default 1.0
*/
max?: number;
/**
* The maximum number of digits to appear after the decimal point, for example `2` will round to 2 decimal points. Only one of `multipleOf`, `precision` or `fractionDigits` should be passed.
* The maximum number of digits to appear after the decimal point, for example `2` will round to 2 decimal points. Only one of `multipleOf` or `fractionDigits` should be passed.
*/
fractionDigits?: number;
/**
* Deprecated alias for `multipleOf`. Only one of `multipleOf`, `precision` or `fractionDigits` should be passed.
*
* @deprecated Use `multipleOf` instead.
*/
precision?: number;
/**
* The generated number will be a multiple of this parameter. Only one of `multipleOf`, `precision` or `fractionDigits` should be passed.
* The generated number will be a multiple of this parameter. Only one of `multipleOf` or `fractionDigits` should be passed.
*/
multipleOf?: number;
} = {}
Expand All @@ -156,23 +147,10 @@ export class NumberModule extends SimpleModuleBase {
min = 0,
max = 1,
fractionDigits,
// eslint-disable-next-line deprecation/deprecation
precision,
// eslint-disable-next-line deprecation/deprecation
multipleOf: originalMultipleOf = precision,
multipleOf = precision ??
(fractionDigits == null ? undefined : 10 ** -fractionDigits),
multipleOf: originalMultipleOf,
multipleOf = fractionDigits == null ? undefined : 10 ** -fractionDigits,
} = options;

if (precision != null) {
deprecated({
deprecated: 'faker.number.float({ precision })',
proposed: 'faker.number.float({ multipleOf })',
since: '8.4',
until: '9.0',
});
}

if (max === min) {
return min;
}
Expand Down Expand Up @@ -201,8 +179,7 @@ export class NumberModule extends SimpleModuleBase {

if (multipleOf != null) {
if (multipleOf <= 0) {
// TODO @xDivisionByZerox: Clean up in v9.0
throw new FakerError(`multipleOf/precision should be greater than 0.`);
throw new FakerError(`multipleOf should be greater than 0.`);
}

const logPrecision = Math.log10(multipleOf);
Expand Down
6 changes: 0 additions & 6 deletions test/modules/__snapshots__/number.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ exports[`number > 42 > float > with min, max and fractionDigits 1`] = `-0.4261`;

exports[`number > 42 > float > with min, max and multipleOf 1`] = `-0.4261`;

exports[`number > 42 > float > with min, max and precision 1`] = `-0.4261`;

exports[`number > 42 > float > with plain number 1`] = `1.49816047538945`;

exports[`number > 42 > hex > noArgs 1`] = `"5"`;
Expand Down Expand Up @@ -82,8 +80,6 @@ exports[`number > 1211 > float > with min, max and fractionDigits 1`] = `61.0658

exports[`number > 1211 > float > with min, max and multipleOf 1`] = `61.0658`;

exports[`number > 1211 > float > with min, max and precision 1`] = `61.0658`;

exports[`number > 1211 > float > with plain number 1`] = `3.714080615610337`;

exports[`number > 1211 > hex > noArgs 1`] = `"e"`;
Expand Down Expand Up @@ -134,8 +130,6 @@ exports[`number > 1337 > float > with min, max and fractionDigits 1`] = `-12.915

exports[`number > 1337 > float > with min, max and multipleOf 1`] = `-12.9153`;

exports[`number > 1337 > float > with min, max and precision 1`] = `-12.9153`;

exports[`number > 1337 > float > with plain number 1`] = `1.0480987000623267`;

exports[`number > 1337 > hex > noArgs 1`] = `"4"`;
Expand Down
83 changes: 2 additions & 81 deletions test/modules/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ describe('number', () => {
.it('with min', { min: -42 })
.it('with max', { max: 69 })
.it('with min and max', { min: -42, max: 69 })
.it('with min, max and precision', {
min: -42,
max: 69,
precision: 0.0001,
})
.it('with min, max and fractionDigits', {
min: -42,
max: 69,
Expand Down Expand Up @@ -238,22 +233,6 @@ describe('number', () => {
}
});

it('provides numbers with a given precision of 0.5 steps', () => {
const results = [
...new Set(
Array.from({ length: 50 }, () =>
faker.number.float({
min: 0,
max: 1.5,
precision: 0.5,
})
)
),
].sort();

expect(results).toEqual([0, 0.5, 1, 1.5]);
});

it('provides numbers with a given multipleOf of 0.5 steps', () => {
const results = [
...new Set(
Expand All @@ -270,22 +249,6 @@ describe('number', () => {
expect(results).toEqual([0, 0.5, 1, 1.5]);
});

it('provides numbers with a given precision of 0.4 steps', () => {
const results = [
...new Set(
Array.from({ length: 50 }, () =>
faker.number.float({
min: 0,
max: 1.9,
precision: 0.4,
})
)
),
].sort();

expect(results).toEqual([0, 0.4, 0.8, 1.2, 1.6]);
});

it.each(times(100))(
'provides numbers with an exact fractional digits',
() => {
Expand Down Expand Up @@ -325,57 +288,15 @@ describe('number', () => {
);
});

it('provides numbers with a given precision of 0.2', () => {
const results = [
...new Set(
Array.from({ length: 50 }, () =>
faker.number.float({
min: 0,
max: 0.4,
precision: 0.2,
})
)
),
].sort();

expect(results).toEqual([0, 0.2, 0.4]);
});

it.each(times(18))(
`provides numbers with an exact precision of 10^-%d`,
(exponent) => {
for (let i = 0; i < 100; i++) {
const actual = faker.number.float({
min: 0.5,
max: 0.99,
precision: 10 ** -exponent,
});
expect(actual).toBe(Number(actual.toFixed(exponent)));
}
}
);

it('throws an error for precision 0', () => {
expect(() => faker.number.float({ precision: 0 })).toThrow(
new FakerError('multipleOf/precision should be greater than 0.')
);
});

it('throws an error for multipleOf 0', () => {
expect(() => faker.number.float({ multipleOf: 0 })).toThrow(
new FakerError('multipleOf/precision should be greater than 0.')
);
});

it('throws an error for negative precision', () => {
expect(() => faker.number.float({ precision: -0.01 })).toThrow(
new FakerError('multipleOf/precision should be greater than 0.')
new FakerError('multipleOf should be greater than 0.')
);
});

it('throws an error for negative multipleOf', () => {
expect(() => faker.number.float({ multipleOf: -0.01 })).toThrow(
new FakerError('multipleOf/precision should be greater than 0.')
new FakerError('multipleOf should be greater than 0.')
);
});

Expand Down