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

feat(number)!: change float default params #1642

Merged
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
14 changes: 7 additions & 7 deletions src/modules/color/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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 }));
}
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 12 additions & 3 deletions src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,24 @@ 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()',
proposed: 'faker.number.float()',
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 });
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions src/modules/number/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions test/__snapshots__/number.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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"`;

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

exports[`number > 1211 > float > with max 1`] = `64.07`;
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved

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"`;

Expand Down Expand Up @@ -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"`;

Expand Down
39 changes: 20 additions & 19 deletions test/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -143,36 +143,37 @@ 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', () => {
expect(faker.number.float({ max: 0 })).toBe(0);
});

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);
}
});

Expand Down Expand Up @@ -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', () => {
Expand Down