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): move methods to new module #1122

Merged
merged 56 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
930d38d
feat(modules): NumberModule
xDivisionByZerox Jun 30, 2022
cd32476
feat(number): int
xDivisionByZerox Jun 30, 2022
51cd8d7
feat(number): hex
xDivisionByZerox Jun 30, 2022
157c886
feat(number): bigInt
xDivisionByZerox Jun 30, 2022
60f7d6f
feat(number): float
xDivisionByZerox Jun 30, 2022
9b59ee7
refactor: replace datatype.float with number.float
xDivisionByZerox Jun 30, 2022
487adc2
refactor(number.float): implementation
xDivisionByZerox Jul 1, 2022
d0ab4cc
refactor(number.hex): give more of a number vibe
xDivisionByZerox Jul 18, 2022
061a11e
test: update snapshots
xDivisionByZerox Aug 18, 2022
9a5941c
test(number): fix unknown function name
xDivisionByZerox Aug 18, 2022
36828af
refactor(helpers): use float instead of int workaround
xDivisionByZerox Aug 20, 2022
a5c70a9
docs(datatype): fix bigInt JSDoc examples
xDivisionByZerox Aug 20, 2022
ead4377
chore(number): unify codestyle
xDivisionByZerox Aug 20, 2022
d4b7b45
test(number): use seededTests factory
xDivisionByZerox Aug 24, 2022
cee7c0a
chore(number.int): simplify option destruction
xDivisionByZerox Aug 24, 2022
400a9ea
chore(number.bigInt): simplify option destruction
xDivisionByZerox Aug 24, 2022
4d6a044
refactor(number): return directly without variable declaration
xDivisionByZerox Aug 24, 2022
97f439b
chore(helper.arrayElements): use number module functions
xDivisionByZerox Aug 24, 2022
fa667a4
test(helpers): update snapshot
xDivisionByZerox Aug 24, 2022
2353375
refactor(helpers): fix arrayElements returning undefined
xDivisionByZerox Aug 24, 2022
67b93db
test(number): extend int/hex tests
xDivisionByZerox Sep 9, 2022
a18de6f
test(number): update snapshots
xDivisionByZerox Sep 9, 2022
58efcba
docs(number): add @since
xDivisionByZerox Sep 9, 2022
9ffaf18
docs(number): fix @examples
xDivisionByZerox Sep 9, 2022
a578576
docs(number): more explicit hex decription
xDivisionByZerox Sep 10, 2022
2886267
test(number): test throw when min > max
xDivisionByZerox Sep 10, 2022
2b6421e
chore: fix mersenne call
Shinigami92 Nov 22, 2022
1a72fa7
chore: some formatting
Shinigami92 Nov 22, 2022
4485cd0
refactor: add deprecations
Shinigami92 Nov 22, 2022
9b93654
chore: replace deprecated method calls
Shinigami92 Nov 22, 2022
87bcbea
docs: generate api-docs
Shinigami92 Nov 22, 2022
7f41fc7
chore: use ** operator
Shinigami92 Nov 22, 2022
c9bf8ba
chore: unify code
Shinigami92 Nov 22, 2022
b12d193
chore: simplify some calls
Shinigami92 Nov 22, 2022
d85b757
chore: simplify some calls
Shinigami92 Nov 22, 2022
44fedff
chore: apply review suggestion
Shinigami92 Nov 22, 2022
87ccced
chore: revert arrayElements change
Shinigami92 Nov 22, 2022
8413139
test: add more tests
Shinigami92 Nov 22, 2022
58ff5fc
test: simplify
Shinigami92 Nov 22, 2022
02ad6b3
test: order
Shinigami92 Nov 22, 2022
b63782e
chore: apply suggestions from code review
Shinigami92 Nov 23, 2022
45003a0
chore: forward bigint call
Shinigami92 Nov 23, 2022
62dee2e
chore: apply review suggestions
Shinigami92 Nov 23, 2022
d156d6a
chore: apply review suggestions
Shinigami92 Nov 23, 2022
dd44c2f
test: copy over missing use-case
Shinigami92 Nov 23, 2022
9286d56
test: use log10 example
Shinigami92 Nov 23, 2022
f2cf2ce
test: add precision 0 and negative test cases
Shinigami92 Nov 23, 2022
ffd6abd
Merge branch 'next' into feat/modules/number
Shinigami92 Nov 23, 2022
158ad4f
chore: apply review suggestion
Shinigami92 Nov 23, 2022
e289031
chore: apply review suggestions
Shinigami92 Nov 23, 2022
df95efc
test: add test case
Shinigami92 Nov 23, 2022
01bf81e
test: add todo tests
Shinigami92 Nov 24, 2022
ae00218
test: improved test
Shinigami92 Nov 24, 2022
1b436af
chore: revert float precision change
Shinigami92 Nov 24, 2022
c59eac2
chore: use new methods
Shinigami92 Nov 24, 2022
237b81d
Merge branch 'next' into feat/modules/number
Shinigami92 Nov 25, 2022
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ If you want consistent results, you can set your own seed:
```ts
faker.seed(123);

const firstRandom = faker.datatype.number();
const firstRandom = faker.number.int();

// Setting the seed again resets the sequence.
faker.seed(123);

const secondRandom = faker.datatype.number();
const secondRandom = faker.number.int();

console.log(firstRandom === secondRandom);
```
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/api-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const apiPages = [
{ text: 'Location', link: '/api/location.html' },
{ text: 'Lorem', link: '/api/lorem.html' },
{ text: 'Music', link: '/api/music.html' },
{ text: 'Number', link: '/api/number.html' },
{ text: 'Person', link: '/api/person.html' },
{ text: 'Phone', link: '/api/phone.html' },
{ text: 'Random', link: '/api/random.html' },
Expand Down
18 changes: 10 additions & 8 deletions src/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { LocationModule as AddressModule } from './modules/location';
import { LocationModule } from './modules/location';
import { LoremModule } from './modules/lorem';
import { MusicModule } from './modules/music';
import { NumberModule } from './modules/number';
import type { PersonModule as NameModule } from './modules/person';
import { PersonModule } from './modules/person';
import { PhoneModule } from './modules/phone';
Expand Down Expand Up @@ -103,6 +104,7 @@ export class Faker {
readonly lorem: LoremModule = new LoremModule(this);
readonly music: MusicModule = new MusicModule(this);
readonly person: PersonModule = new PersonModule(this);
readonly number: NumberModule = new NumberModule(this);
readonly phone: PhoneModule = new PhoneModule(this);
readonly science: ScienceModule = new ScienceModule(this);
readonly string: StringModule = new StringModule(this);
Expand Down Expand Up @@ -239,12 +241,12 @@ export class Faker {
* @example
* // Consistent values for tests:
* faker.seed(42)
* faker.datatype.number(10); // 4
* faker.datatype.number(10); // 8
* faker.number.int(10); // 4
* faker.number.int(10); // 8
*
* faker.seed(42)
* faker.datatype.number(10); // 4
* faker.datatype.number(10); // 8
* faker.number.int(10); // 4
* faker.number.int(10); // 8
*
* @example
* // Random but reproducible tests:
Expand All @@ -271,12 +273,12 @@ export class Faker {
* @example
* // Consistent values for tests:
* faker.seed([42, 13, 17])
* faker.datatype.number(10); // 4
* faker.datatype.number(10); // 8
* faker.number.int(10); // 4
* faker.number.int(10); // 8
*
* faker.seed([42, 13, 17])
* faker.datatype.number(10); // 4
* faker.datatype.number(10); // 8
* faker.number.int(10); // 4
* faker.number.int(10); // 8
*
* @example
* // Random but reproducible tests:
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type {
} from './modules/location';
export type { LoremModule } from './modules/lorem';
export type { MusicModule } from './modules/music';
export type { NumberModule } from './modules/number';
export { Sex } from './modules/person';
export type {
/** @deprecated Use PersonModule instead */
Expand Down
34 changes: 12 additions & 22 deletions src/modules/color/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,9 @@ export class ColorModule {
color = formatHexColor(color, options);
return color;
}
color = Array.from({ length: 3 }).map(() =>
this.faker.datatype.number({ min: 0, max: 255 })
);
color = Array.from({ length: 3 }).map(() => this.faker.number.int(255));
if (includeAlpha) {
color.push(
this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 })
);
color.push(this.faker.number.float({ max: 1, precision: 0.01 }));
cssFunction = 'rgba';
}
return toColorFormat(color, format, cssFunction);
Expand Down Expand Up @@ -385,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 }).map(() =>
this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 })
this.faker.number.float({ max: 1, precision: 0.01 })
);
return toColorFormat(color, options?.format || 'decimal', 'cmyk');
}
Expand Down Expand Up @@ -460,9 +456,9 @@ export class ColorModule {
format?: ColorFormat;
includeAlpha?: boolean;
}): string | number[] {
const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })];
const hsl: number[] = [this.faker.number.int(360)];
for (let i = 0; i < (options?.includeAlpha ? 3 : 2); i++) {
hsl.push(this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 }));
hsl.push(this.faker.number.float({ max: 1, precision: 0.01 }));
}
return toColorFormat(
hsl,
Expand Down Expand Up @@ -537,9 +533,9 @@ export class ColorModule {
* @since 7.0.0
*/
hwb(options?: { format?: ColorFormat }): string | number[] {
const hsl: number[] = [this.faker.datatype.number({ min: 0, max: 360 })];
const hsl: number[] = [this.faker.number.int(360)];
for (let i = 0; i < 2; i++) {
hsl.push(this.faker.datatype.float({ min: 0, max: 1, precision: 0.01 }));
hsl.push(this.faker.number.float({ max: 1, precision: 0.01 }));
}
return toColorFormat(hsl, options?.format || 'decimal', 'hwb');
}
Expand Down Expand Up @@ -596,12 +592,10 @@ export class ColorModule {
*/
lab(options?: { format?: ColorFormat }): string | number[];
lab(options?: { format?: ColorFormat }): string | number[] {
const lab = [
this.faker.datatype.float({ min: 0, max: 1, precision: 0.000001 }),
];
const lab = [this.faker.number.float({ max: 1, precision: 0.000001 })];
for (let i = 0; i < 2; i++) {
lab.push(
this.faker.datatype.float({ min: -100, max: 100, precision: 0.0001 })
this.faker.number.float({ min: -100, max: 100, precision: 0.0001 })
);
}
return toColorFormat(lab, options?.format || 'decimal', 'lab');
Expand Down Expand Up @@ -671,13 +665,9 @@ export class ColorModule {
*/
lch(options?: { format?: ColorFormat }): string | number[];
lch(options?: { format?: ColorFormat }): string | number[] {
const lch = [
this.faker.datatype.float({ min: 0, max: 1, precision: 0.000001 }),
];
const lch = [this.faker.number.float({ max: 1, precision: 0.000001 })];
for (let i = 0; i < 2; i++) {
lch.push(
this.faker.datatype.number({ min: 0, max: 230, precision: 0.1 })
);
lch.push(this.faker.number.float({ max: 230, precision: 0.1 }));
}
return toColorFormat(lch, options?.format || 'decimal', 'lch');
}
Expand Down Expand Up @@ -753,7 +743,7 @@ export class ColorModule {
options = { ...options, space: 'sRGB' };
}
const color = Array.from({ length: 3 }).map(() =>
this.faker.datatype.float({ min: 0, max: 1, precision: 0.0001 })
this.faker.number.float({ max: 1, precision: 0.0001 })
);
return toColorFormat(
color,
Expand Down
10 changes: 3 additions & 7 deletions src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,10 @@ export class CommerceModule {
return `${symbol}${0.0}`;
}

const randValue = this.faker.datatype.number({ max: max, min: min });
// TODO @Shinigami92 2022-11-24: https://github.com/faker-js/faker/issues/350
const randValue = this.faker.number.int({ min, max });

return (
symbol +
(Math.round(randValue * Math.pow(10, dec)) / Math.pow(10, dec)).toFixed(
dec
)
);
return symbol + randValue.toFixed(dec);
}

/**
Expand Down
111 changes: 36 additions & 75 deletions src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { Faker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
import type { Mersenne } from '../../internal/mersenne/mersenne';

/**
* Module to generate various primitive values and data types.
Expand Down Expand Up @@ -37,36 +35,26 @@ export class DatatypeModule {
* faker.datatype.number({ min: 10, max: 100, precision: 0.01 }) // 36.94
*
* @since 5.5.0
*
* @deprecated Use `faker.number.int()` instead.
*/
number(
options: number | { min?: number; max?: number; precision?: number } = 99999
): number {
deprecated({
deprecated: 'faker.datatype.number()',
proposed: 'faker.number.int()',
since: '8.0',
until: '9.0',
});

if (typeof options === 'number') {
options = { max: options };
}

const { min = 0, precision = 1 } = options;
const max = options.max ?? min + 99999;

if (max === min) {
return min;
}

if (max < min) {
throw new FakerError(`Max ${max} should be greater than min ${min}.`);
}
const { min = 0, max = min + 99999, precision = 1 } = options;

const mersenne: Mersenne =
// @ts-expect-error: access private member field
this.faker._mersenne;

const randomNumber = mersenne.next({
min: min / precision,
max: max / precision + 1,
});

// Workaround problem in float point arithmetics for e.g. 6681493 / 0.01
return randomNumber / (1 / precision);
return this.faker.number.float({ min, max, precision });
}

/**
Expand All @@ -86,24 +74,19 @@ export class DatatypeModule {
* faker.datatype.float({ min: 10, max: 100, precision: 0.001 }) // 57.315
*
* @since 5.5.0
*
* @deprecated Use `faker.number.float()` instead.
*/
float(
options?: number | { min?: number; max?: number; precision?: number }
): number {
if (typeof options === 'number') {
options = {
precision: options,
};
}
options = options || {};
const opts: { precision?: number } = {};
for (const p in options) {
opts[p] = options[p];
}
if (opts.precision == null) {
opts.precision = 0.01;
}
return this.number(opts);
deprecated({
deprecated: 'faker.datatype.float()',
proposed: 'faker.number.float()',
since: '8.0',
until: '9.0',
});
return this.faker.number.float(options);
}

/**
Expand Down Expand Up @@ -139,7 +122,7 @@ export class DatatypeModule {
max = Date.UTC(2100, 0);
}

return new Date(this.number({ min, max }));
return new Date(this.faker.number.int({ min, max }));
}

/**
Expand Down Expand Up @@ -177,7 +160,7 @@ export class DatatypeModule {
*
* @since 5.5.0
*
* @deprecated Use faker.string.uuid() instead.
* @deprecated Use `faker.string.uuid()` instead.
*/
uuid(): string {
deprecated({
Expand Down Expand Up @@ -222,7 +205,7 @@ export class DatatypeModule {
// This check is required to avoid returning false when float() returns 1
return true;
}
return this.float({ min: 0, max: 1 }) < probability;
return this.faker.number.float({ max: 1 }) < probability;
}

/**
Expand All @@ -247,7 +230,7 @@ export class DatatypeModule {
*
* @since 6.1.2
*
* @deprecated Use `faker.string.hexadecimal()` instead.
* @deprecated Use `faker.string.hexadecimal()` or `faker.number.hex()` instead.
*/
hexadecimal(
options: {
Expand All @@ -258,7 +241,7 @@ export class DatatypeModule {
): string {
deprecated({
deprecated: 'faker.datatype.hexadecimal()',
proposed: 'faker.string.hexadecimal()',
proposed: 'faker.string.hexadecimal() or faker.number.hex()',
since: '8.0',
until: '9.0',
});
Expand All @@ -280,7 +263,7 @@ export class DatatypeModule {
properties.forEach((prop) => {
returnObject[prop] = this.boolean()
? this.faker.string.sample()
: this.number();
: this.faker.number.int();
});

return JSON.stringify(returnObject);
Expand All @@ -299,7 +282,7 @@ export class DatatypeModule {
*/
array(length = 10): Array<string | number> {
return Array.from<string | number>({ length }).map(() =>
this.boolean() ? this.faker.string.sample() : this.number()
this.boolean() ? this.faker.string.sample() : this.faker.number.int()
);
}

Expand All @@ -320,6 +303,8 @@ export class DatatypeModule {
* faker.datatype.bigInt({ min: 10n, max: 100n }) // 36n
*
* @since 6.0.0
*
* @deprecated Use `faker.number.bigInt()` instead.
*/
bigInt(
options?:
Expand All @@ -332,36 +317,12 @@ export class DatatypeModule {
max?: bigint | boolean | number | string;
}
): bigint {
let min: bigint;
let max: bigint;

if (typeof options === 'object') {
min = BigInt(options.min ?? 0);
max = BigInt(options.max ?? min + BigInt(999999999999999));
} else {
min = BigInt(0);
max = BigInt(options ?? 999999999999999);
}

if (max === min) {
return min;
}

if (max < min) {
throw new FakerError(`Max ${max} should be larger then min ${min}.`);
}

const delta = max - min;

const offset =
BigInt(
this.faker.string.numeric({
length: delta.toString(10).length,
allowLeadingZeros: true,
})
) %
(delta + BigInt(1));

return min + offset;
deprecated({
deprecated: 'faker.datatype.bigInt()',
proposed: 'faker.number.bigInt()',
since: '8.0',
until: '9.0',
});
return this.faker.number.bigInt(options);
}
}
Loading