Skip to content

Commit

Permalink
Merge branch 'next' into 1441-add-global-locale
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Feb 6, 2023
2 parents e06f873 + 8766fd7 commit 7bb31d8
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 8 deletions.
135 changes: 131 additions & 4 deletions src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../../faker';
import { deprecated } from '../../internal/deprecated';

/**
* Module to generate commerce and product related entries.
Expand Down Expand Up @@ -41,6 +42,50 @@ export class CommerceModule {
return `${this.productAdjective()} ${this.productMaterial()} ${this.product()}`;
}

/**
* Generates a price between min and max (inclusive).
*
* @param options An options object. Defaults to `{}`.
* @param options.min The minimum price. Defaults to `1`.
* @param options.max The maximum price. Defaults to `1000`.
* @param options.dec The number of decimal places. Defaults to `2`.
* @param options.symbol The currency value to use. Defaults to `''`.
*
* @example
* faker.commerce.price() // 828.00
* faker.commerce.price({ min: 100 }) // 904.00
* faker.commerce.price({ min: 100, max: 200 }) // 154.00
* faker.commerce.price({ min: 100, max: 200, dec: 0 }) // 133
* faker.commerce.price({ min: 100, max: 200, dec: 0, symbol: '$' }) // $114
*
* @since 3.0.0
*/
price(options?: {
/**
* The minimum price.
*
* @default 1
*/
min?: number;
/**
* The maximum price.
*
* @default 1000
*/
max?: number;
/**
* The number of decimal places.
*
* @default 2
*/
dec?: number;
/**
* The currency value to use.
*
* @default ''
*/
symbol?: string;
}): string;
/**
* Generates a price between min and max (inclusive).
*
Expand All @@ -57,13 +102,95 @@ export class CommerceModule {
* faker.commerce.price(100, 200, 0, '$') // $114
*
* @since 3.0.0
*
* @deprecated Use `faker.commerce.price({ min, max, dec, symbol })` instead.
*/
price(min?: number, max?: number, dec?: number, symbol?: string): string;
/**
* Generates a price between min and max (inclusive).
*
* @param options The minimum price or on options object. Defaults to `{}`.
* @param options.min The minimum price. Defaults to `1`.
* @param options.max The maximum price. Defaults to `1000`.
* @param options.dec The number of decimal places. Defaults to `2`.
* @param options.symbol The currency value to use. Defaults to `''`.
* @param legacyMax The maximum price. This argument is deprecated. Defaults to `1000`.
* @param legacyDec The number of decimal places. This argument is deprecated. Defaults to `2`.
* @param legacySymbol The currency value to use. This argument is deprecated. Defaults to `''`.
*
* @example
* faker.commerce.price() // 828.00
* faker.commerce.price({ min: 100 }) // 904.00
* faker.commerce.price({ min: 100, max: 200 }) // 154.00
* faker.commerce.price({ min: 100, max: 200, dec: 0 }) // 133
* faker.commerce.price({ min: 100, max: 200, dec: 0, symbol: '$' }) // $114
*
* @since 3.0.0
*/
price(
options?:
| number
| {
min?: number;
max?: number;
dec?: number;
symbol?: string;
},
legacyMax?: number,
legacyDec?: number,
legacySymbol?: string
): string;
/**
* Generates a price between min and max (inclusive).
*
* @param options The minimum price or on options object. Defaults to `{}`.
* @param options.min The minimum price. Defaults to `1`.
* @param options.max The maximum price. Defaults to `1000`.
* @param options.dec The number of decimal places. Defaults to `2`.
* @param options.symbol The currency value to use. Defaults to `''`.
* @param legacyMax The maximum price. This argument is deprecated. Defaults to `1000`.
* @param legacyDec The number of decimal places. This argument is deprecated. Defaults to `2`.
* @param legacySymbol The currency value to use. This argument is deprecated. Defaults to `''`.
*
* @example
* faker.commerce.price() // 828.00
* faker.commerce.price({ min: 100 }) // 904.00
* faker.commerce.price({ min: 100, max: 200 }) // 154.00
* faker.commerce.price({ min: 100, max: 200, dec: 0 }) // 133
* faker.commerce.price({ min: 100, max: 200, dec: 0, symbol: '$' }) // $114
*
* @since 3.0.0
*/
price(
min: number = 1,
max: number = 1000,
dec: number = 2,
symbol: string = ''
options:
| number
| {
min?: number;
max?: number;
dec?: number;
symbol?: string;
} = {},
legacyMax: number = 1000,
legacyDec: number = 2,
legacySymbol: string = ''
): string {
if (typeof options === 'number') {
deprecated({
deprecated: 'faker.commerce.price(min, max, dec, symbol)',
proposed: 'faker.commerce.price({ min, max, dec, symbol })',
since: '8.0',
until: '9.0',
});
options = {
min: options,
dec: legacyDec,
max: legacyMax,
symbol: legacySymbol,
};
}

const { dec = 2, max = 1000, min = 1, symbol = '' } = options;

if (min < 0 || max < 0) {
return `${symbol}${0.0}`;
}
Expand Down
36 changes: 33 additions & 3 deletions test/__snapshots__/commerce.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ exports[`commerce > 42 > department 1`] = `"Tools"`;

exports[`commerce > 42 > price > noArgs 1`] = `"375.00"`;

exports[`commerce > 42 > price > with max 1`] = `"38.00"`;
exports[`commerce > 42 > price > with max 1`] = `"375.00"`;

exports[`commerce > 42 > price > with max option 1`] = `"501.00"`;

exports[`commerce > 42 > price > with min 1`] = `"406.00"`;

Expand All @@ -14,6 +16,14 @@ exports[`commerce > 42 > price > with min and max and decimals 1`] = `"69.0000"`

exports[`commerce > 42 > price > with min and max and decimals and symbol 1`] = `"$69.0000"`;

exports[`commerce > 42 > price > with min and max and decimals and symbol option 1`] = `"$69.0000"`;

exports[`commerce > 42 > price > with min and max and decimals option 1`] = `"69.0000"`;

exports[`commerce > 42 > price > with min and max option 1`] = `"69.00"`;

exports[`commerce > 42 > price > with min option 1`] = `"401.00"`;

exports[`commerce > 42 > product 1`] = `"Pants"`;

exports[`commerce > 42 > productAdjective 1`] = `"Fantastic"`;
Expand All @@ -28,7 +38,9 @@ exports[`commerce > 1211 > department 1`] = `"Automotive"`;

exports[`commerce > 1211 > price > noArgs 1`] = `"929.00"`;

exports[`commerce > 1211 > price > with max 1`] = `"93.00"`;
exports[`commerce > 1211 > price > with max 1`] = `"929.00"`;

exports[`commerce > 1211 > price > with max option 1`] = `"1242.00"`;

exports[`commerce > 1211 > price > with min 1`] = `"933.00"`;

Expand All @@ -38,6 +50,14 @@ exports[`commerce > 1211 > price > with min and max and decimals 1`] = `"97.0000

exports[`commerce > 1211 > price > with min and max and decimals and symbol 1`] = `"$97.0000"`;

exports[`commerce > 1211 > price > with min and max and decimals and symbol option 1`] = `"$97.0000"`;

exports[`commerce > 1211 > price > with min and max and decimals option 1`] = `"97.0000"`;

exports[`commerce > 1211 > price > with min and max option 1`] = `"97.00"`;

exports[`commerce > 1211 > price > with min option 1`] = `"932.00"`;

exports[`commerce > 1211 > product 1`] = `"Sausages"`;

exports[`commerce > 1211 > productAdjective 1`] = `"Unbranded"`;
Expand All @@ -52,7 +72,9 @@ exports[`commerce > 1337 > department 1`] = `"Computers"`;

exports[`commerce > 1337 > price > noArgs 1`] = `"263.00"`;

exports[`commerce > 1337 > price > with max 1`] = `"27.00"`;
exports[`commerce > 1337 > price > with max 1`] = `"263.00"`;

exports[`commerce > 1337 > price > with max option 1`] = `"351.00"`;

exports[`commerce > 1337 > price > with min 1`] = `"299.00"`;

Expand All @@ -62,6 +84,14 @@ exports[`commerce > 1337 > price > with min and max and decimals 1`] = `"63.0000

exports[`commerce > 1337 > price > with min and max and decimals and symbol 1`] = `"$63.0000"`;

exports[`commerce > 1337 > price > with min and max and decimals and symbol option 1`] = `"$63.0000"`;

exports[`commerce > 1337 > price > with min and max and decimals option 1`] = `"63.0000"`;

exports[`commerce > 1337 > price > with min and max option 1`] = `"63.00"`;

exports[`commerce > 1337 > price > with min option 1`] = `"293.00"`;

exports[`commerce > 1337 > product 1`] = `"Ball"`;

exports[`commerce > 1337 > productAdjective 1`] = `"Incredible"`;
Expand Down
16 changes: 15 additions & 1 deletion test/commerce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,21 @@ describe('commerce', () => {
.it('with max', undefined, 100)
.it('with min and max', 50, 100)
.it('with min and max and decimals', 50, 100, 4)
.it('with min and max and decimals and symbol', 50, 100, 4, '$');
.it('with min and max and decimals and symbol', 50, 100, 4, '$')
.it('with min option', { min: 42 })
.it('with max option', { max: 1337 })
.it('with min and max option', { min: 50, max: 100 })
.it('with min and max and decimals option', {
min: 50,
max: 100,
dec: 4,
})
.it('with min and max and decimals and symbol option', {
min: 50,
max: 100,
dec: 4,
symbol: '$',
});
});
});

Expand Down

0 comments on commit 7bb31d8

Please sign in to comment.