Skip to content

Commit 671631b

Browse files
authored
feat(number)!: change float default params (#1642)
1 parent 6baa8ce commit 671631b

6 files changed

Lines changed: 57 additions & 49 deletions

File tree

src/modules/color/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ export class ColorModule {
322322
}
323323
color = Array.from({ length: 3 }, () => this.faker.number.int(255));
324324
if (includeAlpha) {
325-
color.push(this.faker.number.float({ max: 1, precision: 0.01 }));
325+
color.push(this.faker.number.float());
326326
cssFunction = 'rgba';
327327
}
328328
return toColorFormat(color, format, cssFunction);
@@ -381,7 +381,7 @@ export class ColorModule {
381381
cmyk(options?: { format?: ColorFormat }): string | number[];
382382
cmyk(options?: { format?: ColorFormat }): string | number[] {
383383
const color: string | number[] = Array.from({ length: 4 }, () =>
384-
this.faker.number.float({ max: 1, precision: 0.01 })
384+
this.faker.number.float()
385385
);
386386
return toColorFormat(color, options?.format || 'decimal', 'cmyk');
387387
}
@@ -458,7 +458,7 @@ export class ColorModule {
458458
}): string | number[] {
459459
const hsl: number[] = [this.faker.number.int(360)];
460460
for (let i = 0; i < (options?.includeAlpha ? 3 : 2); i++) {
461-
hsl.push(this.faker.number.float({ max: 1, precision: 0.01 }));
461+
hsl.push(this.faker.number.float());
462462
}
463463
return toColorFormat(
464464
hsl,
@@ -535,7 +535,7 @@ export class ColorModule {
535535
hwb(options?: { format?: ColorFormat }): string | number[] {
536536
const hsl: number[] = [this.faker.number.int(360)];
537537
for (let i = 0; i < 2; i++) {
538-
hsl.push(this.faker.number.float({ max: 1, precision: 0.01 }));
538+
hsl.push(this.faker.number.float());
539539
}
540540
return toColorFormat(hsl, options?.format || 'decimal', 'hwb');
541541
}
@@ -592,7 +592,7 @@ export class ColorModule {
592592
*/
593593
lab(options?: { format?: ColorFormat }): string | number[];
594594
lab(options?: { format?: ColorFormat }): string | number[] {
595-
const lab = [this.faker.number.float({ max: 1, precision: 0.000001 })];
595+
const lab = [this.faker.number.float({ precision: 0.000001 })];
596596
for (let i = 0; i < 2; i++) {
597597
lab.push(
598598
this.faker.number.float({ min: -100, max: 100, precision: 0.0001 })
@@ -665,7 +665,7 @@ export class ColorModule {
665665
*/
666666
lch(options?: { format?: ColorFormat }): string | number[];
667667
lch(options?: { format?: ColorFormat }): string | number[] {
668-
const lch = [this.faker.number.float({ max: 1, precision: 0.000001 })];
668+
const lch = [this.faker.number.float({ precision: 0.000001 })];
669669
for (let i = 0; i < 2; i++) {
670670
lch.push(this.faker.number.float({ max: 230, precision: 0.1 }));
671671
}
@@ -743,7 +743,7 @@ export class ColorModule {
743743
options = { ...options, space: 'sRGB' };
744744
}
745745
const color = Array.from({ length: 3 }, () =>
746-
this.faker.number.float({ max: 1, precision: 0.0001 })
746+
this.faker.number.float({ precision: 0.0001 })
747747
);
748748
return toColorFormat(
749749
color,

src/modules/datatype/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,24 @@ export class DatatypeModule {
8383
* @deprecated Use `faker.number.float()` instead.
8484
*/
8585
float(
86-
options?: number | { min?: number; max?: number; precision?: number }
86+
options: number | { min?: number; max?: number; precision?: number } = {}
8787
): number {
8888
deprecated({
8989
deprecated: 'faker.datatype.float()',
9090
proposed: 'faker.number.float()',
9191
since: '8.0',
9292
until: '9.0',
9393
});
94-
return this.faker.number.float(options);
94+
95+
if (typeof options === 'number') {
96+
options = {
97+
precision: options,
98+
};
99+
}
100+
101+
const { min = 0, max = min + 99999, precision = 0.01 } = options;
102+
103+
return this.faker.number.float({ min, max, precision });
95104
}
96105

97106
/**
@@ -210,7 +219,7 @@ export class DatatypeModule {
210219
// This check is required to avoid returning false when float() returns 1
211220
return true;
212221
}
213-
return this.faker.number.float({ max: 1 }) < probability;
222+
return this.faker.number.float() < probability;
214223
}
215224

216225
/**

src/modules/helpers/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,7 @@ export class HelpersModule {
484484
let index: number;
485485

486486
while (i-- > min) {
487-
index = Math.floor(
488-
(i + 1) * this.faker.number.float({ min: 0, max: 0.99 })
489-
);
487+
index = Math.floor((i + 1) * this.faker.number.float({ max: 0.99 }));
490488
temp = arrayCopy[index];
491489
arrayCopy[index] = arrayCopy[i];
492490
arrayCopy[i] = temp;

src/modules/number/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,18 @@ export class NumberModule {
6262
/**
6363
* Returns a single random floating-point number for a given precision or range and precision.
6464
*
65-
* @param options Precision or options object. Defaults to `{}`.
66-
* @param options.min Lower bound for generated number. Defaults to `0`.
67-
* @param options.max Upper bound for generated number. Defaults to `99999`.
65+
* @param options Upper bound or options object. Defaults to `{}`.
66+
* @param options.min Lower bound for generated number. Defaults to `0.0`.
67+
* @param options.max Upper bound for generated number. Defaults to `1.0`.
6868
* @param options.precision Precision of the generated number. Defaults to `0.01`.
6969
*
7070
* @example
71-
* faker.number.float() // 51696.36
72-
* faker.number.float(1) // 52023.2
73-
* faker.number.float({ min: 1000000 }) // 212859.76
74-
* faker.number.float({ max: 100 }) // 28.11
75-
* faker.number.float({ precision: 0.1 }) // 84055.3
76-
* faker.number.float({ min: 10, max: 100, precision: 0.001 }) // 57.315
71+
* faker.number.float() // 0.89
72+
* faker.number.float(3) // 1.14
73+
* faker.number.float({ min: -1000000 }) // -823469.91
74+
* faker.number.float({ max: 100 }) // 27.28
75+
* faker.number.float({ precision: 0.1 }) // 0.9
76+
* faker.number.float({ min: 10, max: 100, precision: 0.001 }) // 35.415
7777
*
7878
* @since 8.0.0
7979
*/
@@ -82,11 +82,11 @@ export class NumberModule {
8282
): number {
8383
if (typeof options === 'number') {
8484
options = {
85-
precision: options,
85+
max: options,
8686
};
8787
}
8888

89-
const { min = 0, max = min + 99999, precision = 0.01 } = options;
89+
const { min = 0, max = 1, precision = 0.01 } = options;
9090

9191
if (max === min) {
9292
return min;

test/__snapshots__/number.spec.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ exports[`number > 42 > bigInt > with string value 1`] = `37n`;
1616

1717
exports[`number > 42 > float > with max 1`] = `25.84`;
1818

19-
exports[`number > 42 > float > with min 1`] = `37411.64`;
19+
exports[`number > 42 > float > with min 1`] = `-25.9`;
2020

2121
exports[`number > 42 > float > with min and max 1`] = `-0.43`;
2222

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

25-
exports[`number > 42 > float > with plain number 1`] = `37453.636891`;
25+
exports[`number > 42 > float > with plain number 1`] = `1.5`;
2626

2727
exports[`number > 42 > hex > noArgs 1`] = `"6"`;
2828

@@ -52,13 +52,13 @@ exports[`number > 1211 > bigInt > with string value 1`] = `24n`;
5252

5353
exports[`number > 1211 > float > with max 1`] = `64.07`;
5454

55-
exports[`number > 1211 > float > with min 1`] = `92809.09`;
55+
exports[`number > 1211 > float > with min 1`] = `-2.07`;
5656

5757
exports[`number > 1211 > float > with min and max 1`] = `61.07`;
5858

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

61-
exports[`number > 1211 > float > with plain number 1`] = `92851.086855`;
61+
exports[`number > 1211 > float > with plain number 1`] = `3.72`;
6262

6363
exports[`number > 1211 > hex > noArgs 1`] = `"f"`;
6464

@@ -88,13 +88,13 @@ exports[`number > 1337 > bigInt > with string value 1`] = `25n`;
8888

8989
exports[`number > 1337 > float > with max 1`] = `18.08`;
9090

91-
exports[`number > 1337 > float > with min 1`] = `26160.2`;
91+
exports[`number > 1337 > float > with min 1`] = `-30.74`;
9292

9393
exports[`number > 1337 > float > with min and max 1`] = `-12.92`;
9494

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

97-
exports[`number > 1337 > float > with plain number 1`] = `26202.205595`;
97+
exports[`number > 1337 > float > with plain number 1`] = `1.05`;
9898

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

test/number.spec.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('number', () => {
1919
});
2020

2121
t.describe('float', (t) => {
22-
t.it('with plain number', 0.000001)
22+
t.it('with plain number', 4)
2323
.it('with min', { min: -42 })
2424
.it('with max', { max: 69 })
2525
.it('with min and max', { min: -42, max: 69 })
@@ -143,36 +143,37 @@ describe('number', () => {
143143

144144
describe('float', () => {
145145
it('should return a random float with a default precision of 2 digits after floating point', () => {
146-
const number = faker.number.float();
147-
expect(number).toBe(Number(number.toFixed(2)));
146+
const actual = faker.number.float();
147+
expect(actual).toBe(Number(actual.toFixed(2)));
148148
});
149149

150-
it('should return a random float given a precision value', () => {
151-
const number = faker.number.float(0.001);
152-
expect(number).toBe(Number(number.toFixed(3)));
150+
it('should return a random float with given max', () => {
151+
const actual = faker.number.float(3);
152+
expect(actual).toBeGreaterThanOrEqual(0);
153+
expect(actual).toBeLessThanOrEqual(3);
153154
});
154155

155156
it('should return a random number given a max value of 10', () => {
156-
const float = faker.number.float({ max: 10 });
157-
expect(float).toBeGreaterThanOrEqual(0);
158-
expect(float).toBeLessThanOrEqual(10);
157+
const actual = faker.number.float({ max: 10 });
158+
expect(actual).toBeGreaterThanOrEqual(0);
159+
expect(actual).toBeLessThanOrEqual(10);
159160
});
160161

161162
it('should return 0 given a max value of 0', () => {
162163
expect(faker.number.float({ max: 0 })).toBe(0);
163164
});
164165

165166
it('should return a random number given a negative number min and max value of 0', () => {
166-
const float = faker.number.float({ min: -100, max: 0 });
167-
expect(float).toBeGreaterThanOrEqual(-100);
168-
expect(float).toBeLessThanOrEqual(0);
167+
const actual = faker.number.float({ min: -100, max: 0 });
168+
expect(actual).toBeGreaterThanOrEqual(-100);
169+
expect(actual).toBeLessThanOrEqual(0);
169170
});
170171

171172
it('should return a random number between a range', () => {
172173
for (let i = 0; i < 5; i++) {
173-
const randomNumber = faker.number.float({ min: 22, max: 33 });
174-
expect(randomNumber).toBeGreaterThanOrEqual(22);
175-
expect(randomNumber).toBeLessThanOrEqual(33);
174+
const actual = faker.number.float({ min: 22, max: 33 });
175+
expect(actual).toBeGreaterThanOrEqual(22);
176+
expect(actual).toBeLessThanOrEqual(33);
176177
}
177178
});
178179

@@ -211,19 +212,19 @@ describe('number', () => {
211212

212213
it('provides numbers with an exact precision', () => {
213214
for (let i = 0; i < 100; i++) {
214-
const number = faker.number.float({
215+
const actual = faker.number.float({
215216
min: 0.5,
216217
max: 0.99,
217218
precision: 0.01,
218219
});
219-
expect(number).toBe(Number(number.toFixed(2)));
220+
expect(actual).toBe(Number(actual.toFixed(2)));
220221
}
221222
});
222223

223224
it('provides number with a precision 0', () => {
224-
const float = faker.number.float({ precision: 0 });
225+
const actual = faker.number.float({ precision: 0 });
225226

226-
expect(float).toBe(Math.floor(float));
227+
expect(actual).toBe(Math.floor(actual));
227228
});
228229

229230
it('should not modify the input object', () => {

0 commit comments

Comments
 (0)