diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 11ebdcd906c..167fc225591 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -322,7 +322,7 @@ export class ColorModule { } color = Array.from({ length: 3 }, () => this.faker.number.int(255)); if (includeAlpha) { - color.push(this.faker.number.float({ max: 1, precision: 0.01 })); + color.push(this.faker.number.float()); cssFunction = 'rgba'; } return toColorFormat(color, format, cssFunction); @@ -381,7 +381,7 @@ export class ColorModule { cmyk(options?: { format?: ColorFormat }): string | number[]; cmyk(options?: { format?: ColorFormat }): string | number[] { const color: string | number[] = Array.from({ length: 4 }, () => - this.faker.number.float({ max: 1, precision: 0.01 }) + this.faker.number.float() ); return toColorFormat(color, options?.format || 'decimal', 'cmyk'); } @@ -458,7 +458,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({ max: 1, precision: 0.01 })); + hsl.push(this.faker.number.float()); } return toColorFormat( hsl, @@ -535,7 +535,7 @@ export class ColorModule { hwb(options?: { format?: ColorFormat }): string | number[] { const hsl: number[] = [this.faker.number.int(360)]; for (let i = 0; i < 2; i++) { - hsl.push(this.faker.number.float({ max: 1, precision: 0.01 })); + hsl.push(this.faker.number.float()); } return toColorFormat(hsl, options?.format || 'decimal', 'hwb'); } @@ -592,7 +592,7 @@ export class ColorModule { */ lab(options?: { format?: ColorFormat }): string | number[]; lab(options?: { format?: ColorFormat }): string | number[] { - const lab = [this.faker.number.float({ max: 1, precision: 0.000001 })]; + const lab = [this.faker.number.float({ precision: 0.000001 })]; for (let i = 0; i < 2; i++) { lab.push( this.faker.number.float({ min: -100, max: 100, precision: 0.0001 }) @@ -665,7 +665,7 @@ export class ColorModule { */ lch(options?: { format?: ColorFormat }): string | number[]; lch(options?: { format?: ColorFormat }): string | number[] { - const lch = [this.faker.number.float({ max: 1, precision: 0.000001 })]; + const lch = [this.faker.number.float({ precision: 0.000001 })]; for (let i = 0; i < 2; i++) { lch.push(this.faker.number.float({ max: 230, precision: 0.1 })); } @@ -743,7 +743,7 @@ export class ColorModule { options = { ...options, space: 'sRGB' }; } const color = Array.from({ length: 3 }, () => - this.faker.number.float({ max: 1, precision: 0.0001 }) + this.faker.number.float({ precision: 0.0001 }) ); return toColorFormat( color, diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index b7000dc6d5f..f9c2bac6759 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -83,7 +83,7 @@ export class DatatypeModule { * @deprecated Use `faker.number.float()` instead. */ float( - options?: number | { min?: number; max?: number; precision?: number } + options: number | { min?: number; max?: number; precision?: number } = {} ): number { deprecated({ deprecated: 'faker.datatype.float()', @@ -91,7 +91,16 @@ export class DatatypeModule { since: '8.0', until: '9.0', }); - return this.faker.number.float(options); + + if (typeof options === 'number') { + options = { + precision: options, + }; + } + + const { min = 0, max = min + 99999, precision = 0.01 } = options; + + return this.faker.number.float({ min, max, precision }); } /** @@ -210,7 +219,7 @@ export class DatatypeModule { // This check is required to avoid returning false when float() returns 1 return true; } - return this.faker.number.float({ max: 1 }) < probability; + return this.faker.number.float() < probability; } /** diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 37487247298..4cf9ad38b45 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -484,9 +484,7 @@ export class HelpersModule { let index: number; while (i-- > min) { - index = Math.floor( - (i + 1) * this.faker.number.float({ min: 0, max: 0.99 }) - ); + index = Math.floor((i + 1) * this.faker.number.float({ max: 0.99 })); temp = arrayCopy[index]; arrayCopy[index] = arrayCopy[i]; arrayCopy[i] = temp; diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts index 1f46cb64814..292fbcf2e3a 100644 --- a/src/modules/number/index.ts +++ b/src/modules/number/index.ts @@ -62,18 +62,18 @@ export class NumberModule { /** * Returns a single random floating-point number for a given precision or range and precision. * - * @param options Precision or options object. Defaults to `{}`. - * @param options.min Lower bound for generated number. Defaults to `0`. - * @param options.max Upper bound for generated number. Defaults to `99999`. + * @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. Defaults to `0.01`. * * @example - * faker.number.float() // 51696.36 - * faker.number.float(1) // 52023.2 - * faker.number.float({ min: 1000000 }) // 212859.76 - * faker.number.float({ max: 100 }) // 28.11 - * faker.number.float({ precision: 0.1 }) // 84055.3 - * faker.number.float({ min: 10, max: 100, precision: 0.001 }) // 57.315 + * faker.number.float() // 0.89 + * faker.number.float(3) // 1.14 + * faker.number.float({ min: -1000000 }) // -823469.91 + * faker.number.float({ max: 100 }) // 27.28 + * faker.number.float({ precision: 0.1 }) // 0.9 + * faker.number.float({ min: 10, max: 100, precision: 0.001 }) // 35.415 * * @since 8.0.0 */ @@ -82,11 +82,11 @@ export class NumberModule { ): number { if (typeof options === 'number') { options = { - precision: options, + max: options, }; } - const { min = 0, max = min + 99999, precision = 0.01 } = options; + const { min = 0, max = 1, precision = 0.01 } = options; if (max === min) { return min; diff --git a/test/__snapshots__/number.spec.ts.snap b/test/__snapshots__/number.spec.ts.snap index e89a2a0ed15..14b16521ee5 100644 --- a/test/__snapshots__/number.spec.ts.snap +++ b/test/__snapshots__/number.spec.ts.snap @@ -16,13 +16,13 @@ exports[`number > 42 > bigInt > with string value 1`] = `37n`; exports[`number > 42 > float > with max 1`] = `25.84`; -exports[`number > 42 > float > with min 1`] = `37411.64`; +exports[`number > 42 > float > with min 1`] = `-25.9`; exports[`number > 42 > float > with min and max 1`] = `-0.43`; exports[`number > 42 > float > with min, max and precision 1`] = `-0.4261`; -exports[`number > 42 > float > with plain number 1`] = `37453.636891`; +exports[`number > 42 > float > with plain number 1`] = `1.5`; exports[`number > 42 > hex > noArgs 1`] = `"6"`; @@ -52,13 +52,13 @@ exports[`number > 1211 > bigInt > with string value 1`] = `24n`; exports[`number > 1211 > float > with max 1`] = `64.07`; -exports[`number > 1211 > float > with min 1`] = `92809.09`; +exports[`number > 1211 > float > with min 1`] = `-2.07`; exports[`number > 1211 > float > with min and max 1`] = `61.07`; exports[`number > 1211 > float > with min, max and precision 1`] = `61.0658`; -exports[`number > 1211 > float > with plain number 1`] = `92851.086855`; +exports[`number > 1211 > float > with plain number 1`] = `3.72`; exports[`number > 1211 > hex > noArgs 1`] = `"f"`; @@ -88,13 +88,13 @@ exports[`number > 1337 > bigInt > with string value 1`] = `25n`; exports[`number > 1337 > float > with max 1`] = `18.08`; -exports[`number > 1337 > float > with min 1`] = `26160.2`; +exports[`number > 1337 > float > with min 1`] = `-30.74`; exports[`number > 1337 > float > with min and max 1`] = `-12.92`; exports[`number > 1337 > float > with min, max and precision 1`] = `-12.9153`; -exports[`number > 1337 > float > with plain number 1`] = `26202.205595`; +exports[`number > 1337 > float > with plain number 1`] = `1.05`; exports[`number > 1337 > hex > noArgs 1`] = `"4"`; diff --git a/test/number.spec.ts b/test/number.spec.ts index 5ba1a0a4a3d..7605a513cb2 100644 --- a/test/number.spec.ts +++ b/test/number.spec.ts @@ -19,7 +19,7 @@ describe('number', () => { }); t.describe('float', (t) => { - t.it('with plain number', 0.000001) + t.it('with plain number', 4) .it('with min', { min: -42 }) .it('with max', { max: 69 }) .it('with min and max', { min: -42, max: 69 }) @@ -143,19 +143,20 @@ describe('number', () => { describe('float', () => { it('should return a random float with a default precision of 2 digits after floating point', () => { - const number = faker.number.float(); - expect(number).toBe(Number(number.toFixed(2))); + const actual = faker.number.float(); + expect(actual).toBe(Number(actual.toFixed(2))); }); - it('should return a random float given a precision value', () => { - const number = faker.number.float(0.001); - expect(number).toBe(Number(number.toFixed(3))); + it('should return a random float with given max', () => { + const actual = faker.number.float(3); + expect(actual).toBeGreaterThanOrEqual(0); + expect(actual).toBeLessThanOrEqual(3); }); it('should return a random number given a max value of 10', () => { - const float = faker.number.float({ max: 10 }); - expect(float).toBeGreaterThanOrEqual(0); - expect(float).toBeLessThanOrEqual(10); + const actual = faker.number.float({ max: 10 }); + expect(actual).toBeGreaterThanOrEqual(0); + expect(actual).toBeLessThanOrEqual(10); }); it('should return 0 given a max value of 0', () => { @@ -163,16 +164,16 @@ describe('number', () => { }); it('should return a random number given a negative number min and max value of 0', () => { - const float = faker.number.float({ min: -100, max: 0 }); - expect(float).toBeGreaterThanOrEqual(-100); - expect(float).toBeLessThanOrEqual(0); + const actual = faker.number.float({ min: -100, max: 0 }); + expect(actual).toBeGreaterThanOrEqual(-100); + expect(actual).toBeLessThanOrEqual(0); }); it('should return a random number between a range', () => { for (let i = 0; i < 5; i++) { - const randomNumber = faker.number.float({ min: 22, max: 33 }); - expect(randomNumber).toBeGreaterThanOrEqual(22); - expect(randomNumber).toBeLessThanOrEqual(33); + const actual = faker.number.float({ min: 22, max: 33 }); + expect(actual).toBeGreaterThanOrEqual(22); + expect(actual).toBeLessThanOrEqual(33); } }); @@ -211,19 +212,19 @@ describe('number', () => { it('provides numbers with an exact precision', () => { for (let i = 0; i < 100; i++) { - const number = faker.number.float({ + const actual = faker.number.float({ min: 0.5, max: 0.99, precision: 0.01, }); - expect(number).toBe(Number(number.toFixed(2))); + expect(actual).toBe(Number(actual.toFixed(2))); } }); it('provides number with a precision 0', () => { - const float = faker.number.float({ precision: 0 }); + const actual = faker.number.float({ precision: 0 }); - expect(float).toBe(Math.floor(float)); + expect(actual).toBe(Math.floor(actual)); }); it('should not modify the input object', () => {