Skip to content

Commit a672d27

Browse files
authored
refactor(number)!: remove v8 deprecated number parameter (#2738)
1 parent 19bcf88 commit a672d27

File tree

4 files changed

+18
-119
lines changed

4 files changed

+18
-119
lines changed

docs/guide/upgrading_v9/2738.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Remove deprecated number parameter
2+
3+
Removed deprecated number parameter
4+
5+
| old | replacement |
6+
| ----------------------------------- | ------------------------------------ |
7+
| `faker.number.float({ precision })` | `faker.number.float({ multipleOf })` |

src/modules/number/index.ts

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { FakerError } from '../../errors/faker-error';
2-
import { deprecated } from '../../internal/deprecated';
32
import { SimpleModuleBase } from '../../internal/module-base';
43

54
/**
@@ -90,13 +89,11 @@ export class NumberModule extends SimpleModuleBase {
9089
*
9190
* @param options Upper bound or options object.
9291
* @param options.min Lower bound for generated number, inclusive. Defaults to `0.0`.
93-
* @param options.max Upper bound for generated number, exclusive, unless `multipleOf`, `precision` or `fractionDigits` are passed. Defaults to `1.0`.
94-
* @param options.precision Deprecated alias for `multipleOf`. Only one of `multipleOf`, `precision` or `fractionDigits` should be passed.
95-
* @param options.multipleOf The generated number will be a multiple of this parameter. Only one of `multipleOf`, `precision` or `fractionDigits` should be passed.
96-
* @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.
92+
* @param options.max Upper bound for generated number, exclusive, unless `multipleOf` or `fractionDigits` are passed. Defaults to `1.0`.
93+
* @param options.multipleOf The generated number will be a multiple of this parameter. Only one of `multipleOf` or `fractionDigits` should be passed.
94+
* @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.
9795
*
9896
* @throws When `min` is greater than `max`.
99-
* @throws When `precision` is negative.
10097
* @throws When `multipleOf` is negative.
10198
* @throws When `fractionDigits` is negative.
10299
* @throws When `fractionDigits` and `multipleOf` is passed in the same options object.
@@ -125,23 +122,17 @@ export class NumberModule extends SimpleModuleBase {
125122
*/
126123
min?: number;
127124
/**
128-
* Upper bound for generated number, exclusive, unless `multipleOf`, `precision` or `fractionDigits` are passed.
125+
* Upper bound for generated number, exclusive, unless `multipleOf` or `fractionDigits` are passed.
129126
*
130127
* @default 1.0
131128
*/
132129
max?: number;
133130
/**
134-
* 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.
131+
* 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.
135132
*/
136133
fractionDigits?: number;
137134
/**
138-
* Deprecated alias for `multipleOf`. Only one of `multipleOf`, `precision` or `fractionDigits` should be passed.
139-
*
140-
* @deprecated Use `multipleOf` instead.
141-
*/
142-
precision?: number;
143-
/**
144-
* The generated number will be a multiple of this parameter. Only one of `multipleOf`, `precision` or `fractionDigits` should be passed.
135+
* The generated number will be a multiple of this parameter. Only one of `multipleOf` or `fractionDigits` should be passed.
145136
*/
146137
multipleOf?: number;
147138
} = {}
@@ -156,23 +147,10 @@ export class NumberModule extends SimpleModuleBase {
156147
min = 0,
157148
max = 1,
158149
fractionDigits,
159-
// eslint-disable-next-line deprecation/deprecation
160-
precision,
161-
// eslint-disable-next-line deprecation/deprecation
162-
multipleOf: originalMultipleOf = precision,
163-
multipleOf = precision ??
164-
(fractionDigits == null ? undefined : 10 ** -fractionDigits),
150+
multipleOf: originalMultipleOf,
151+
multipleOf = fractionDigits == null ? undefined : 10 ** -fractionDigits,
165152
} = options;
166153

167-
if (precision != null) {
168-
deprecated({
169-
deprecated: 'faker.number.float({ precision })',
170-
proposed: 'faker.number.float({ multipleOf })',
171-
since: '8.4',
172-
until: '9.0',
173-
});
174-
}
175-
176154
if (max === min) {
177155
return min;
178156
}
@@ -201,8 +179,7 @@ export class NumberModule extends SimpleModuleBase {
201179

202180
if (multipleOf != null) {
203181
if (multipleOf <= 0) {
204-
// TODO @xDivisionByZerox: Clean up in v9.0
205-
throw new FakerError(`multipleOf/precision should be greater than 0.`);
182+
throw new FakerError(`multipleOf should be greater than 0.`);
206183
}
207184

208185
const logPrecision = Math.log10(multipleOf);

test/modules/__snapshots__/number.spec.ts.snap

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ exports[`number > 42 > float > with min, max and fractionDigits 1`] = `-0.4261`;
3030

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

33-
exports[`number > 42 > float > with min, max and precision 1`] = `-0.4261`;
34-
3533
exports[`number > 42 > float > with plain number 1`] = `1.49816047538945`;
3634

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

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

85-
exports[`number > 1211 > float > with min, max and precision 1`] = `61.0658`;
86-
8783
exports[`number > 1211 > float > with plain number 1`] = `3.714080615610337`;
8884

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

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

137-
exports[`number > 1337 > float > with min, max and precision 1`] = `-12.9153`;
138-
139133
exports[`number > 1337 > float > with plain number 1`] = `1.0480987000623267`;
140134

141135
exports[`number > 1337 > hex > noArgs 1`] = `"4"`;

test/modules/number.spec.ts

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ describe('number', () => {
2323
.it('with min', { min: -42 })
2424
.it('with max', { max: 69 })
2525
.it('with min and max', { min: -42, max: 69 })
26-
.it('with min, max and precision', {
27-
min: -42,
28-
max: 69,
29-
precision: 0.0001,
30-
})
3126
.it('with min, max and fractionDigits', {
3227
min: -42,
3328
max: 69,
@@ -238,22 +233,6 @@ describe('number', () => {
238233
}
239234
});
240235

241-
it('provides numbers with a given precision of 0.5 steps', () => {
242-
const results = [
243-
...new Set(
244-
Array.from({ length: 50 }, () =>
245-
faker.number.float({
246-
min: 0,
247-
max: 1.5,
248-
precision: 0.5,
249-
})
250-
)
251-
),
252-
].sort();
253-
254-
expect(results).toEqual([0, 0.5, 1, 1.5]);
255-
});
256-
257236
it('provides numbers with a given multipleOf of 0.5 steps', () => {
258237
const results = [
259238
...new Set(
@@ -270,22 +249,6 @@ describe('number', () => {
270249
expect(results).toEqual([0, 0.5, 1, 1.5]);
271250
});
272251

273-
it('provides numbers with a given precision of 0.4 steps', () => {
274-
const results = [
275-
...new Set(
276-
Array.from({ length: 50 }, () =>
277-
faker.number.float({
278-
min: 0,
279-
max: 1.9,
280-
precision: 0.4,
281-
})
282-
)
283-
),
284-
].sort();
285-
286-
expect(results).toEqual([0, 0.4, 0.8, 1.2, 1.6]);
287-
});
288-
289252
it.each(times(100))(
290253
'provides numbers with an exact fractional digits',
291254
() => {
@@ -325,57 +288,15 @@ describe('number', () => {
325288
);
326289
});
327290

328-
it('provides numbers with a given precision of 0.2', () => {
329-
const results = [
330-
...new Set(
331-
Array.from({ length: 50 }, () =>
332-
faker.number.float({
333-
min: 0,
334-
max: 0.4,
335-
precision: 0.2,
336-
})
337-
)
338-
),
339-
].sort();
340-
341-
expect(results).toEqual([0, 0.2, 0.4]);
342-
});
343-
344-
it.each(times(18))(
345-
`provides numbers with an exact precision of 10^-%d`,
346-
(exponent) => {
347-
for (let i = 0; i < 100; i++) {
348-
const actual = faker.number.float({
349-
min: 0.5,
350-
max: 0.99,
351-
precision: 10 ** -exponent,
352-
});
353-
expect(actual).toBe(Number(actual.toFixed(exponent)));
354-
}
355-
}
356-
);
357-
358-
it('throws an error for precision 0', () => {
359-
expect(() => faker.number.float({ precision: 0 })).toThrow(
360-
new FakerError('multipleOf/precision should be greater than 0.')
361-
);
362-
});
363-
364291
it('throws an error for multipleOf 0', () => {
365292
expect(() => faker.number.float({ multipleOf: 0 })).toThrow(
366-
new FakerError('multipleOf/precision should be greater than 0.')
367-
);
368-
});
369-
370-
it('throws an error for negative precision', () => {
371-
expect(() => faker.number.float({ precision: -0.01 })).toThrow(
372-
new FakerError('multipleOf/precision should be greater than 0.')
293+
new FakerError('multipleOf should be greater than 0.')
373294
);
374295
});
375296

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

0 commit comments

Comments
 (0)